Skip to content

Commit

Permalink
Added getUser() cal to hooks.server to guarantee the session
Browse files Browse the repository at this point in the history
Added getUser() call as advices by @charislam in this discussion concerning the enancement of  the documentation
supabase/auth-helpers#742 (comment)
  • Loading branch information
kvetoslavnovak authored Feb 14, 2024
1 parent 085eb1a commit f208336
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ Creating a Supabase client with the `ssr` package automatically configures it to

<Admonition type="danger">

Beware when accessing the session object on the server, because it is not revalidated on every request from the client. That means the sender can tamper with unencoded data in the session object. If you need to verify the integrity of user data for server logic, call `auth.getUser` instead, which will query the Supabase Auth server for trusted user data.
Beware when accessing the session object on the server, because it is not revalidated on every request from the client. That means the sender can tamper with unencoded data in the session object. If you need to verify the integrity of user data for server logic, call `auth.getUser` instead, which will query the Supabase Auth server for trusted user data. This risk can be removed if you call getUser() in hooks.server.ts.

</Admonition>

Expand Down Expand Up @@ -480,6 +480,13 @@ export const handle: Handle = async ({ event, resolve }) => {
* you just call this `await getSession()`
*/
event.locals.getSession = async () => {
/**
* getUser will guarantee that the stored session is valid,
* and calling getSession immediately after
* will leave no room for anyone to modify the stored session.
*/
const { data: getUserData, error: err } = await event.locals.supabase.auth.getUser()

const {
data: { session },
} = await event.locals.supabase.auth.getSession()
Expand Down

0 comments on commit f208336

Please sign in to comment.