Skip to content

Commit

Permalink
Get up to date with the SB SK guide: https://supabase.com/docs/guides…
Browse files Browse the repository at this point in the history
  • Loading branch information
scosman committed Aug 29, 2024
1 parent eacd3af commit c8eb9ed
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
28 changes: 18 additions & 10 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ export const handle: Handle = async ({ event, resolve }) => {
PUBLIC_SUPABASE_ANON_KEY,
{
cookies: {
get: (key) => event.cookies.get(key),
getAll: () => event.cookies.getAll(),
/**
* Note: You have to add the `path` variable to the
* set and remove method due to sveltekit's cookie API
* requiring this to be set, setting the path to an empty string
* will replicate previous/standard behaviour (https://kit.svelte.dev/docs/types#public-types-cookies)
* SvelteKit's cookies API requires `path` to be explicitly set in
* the cookie options. Setting `path` to `/` replicates previous/
* standard behavior.
*/
set: (key, value, options) => {
event.cookies.set(key, value, { ...options, path: "/" })
},
remove: (key, options) => {
event.cookies.delete(key, { ...options, path: "/" })
setAll: (cookiesToSet) => {
cookiesToSet.forEach(({ name, value, options }) => {
event.cookies.set(name, value, { ...options, path: "/" })
})
},
},
},
Expand All @@ -37,6 +35,16 @@ export const handle: Handle = async ({ event, resolve }) => {
{ auth: { persistSession: false } },
)

// https://github.com/supabase/auth-js/issues/888#issuecomment-2189298518
if ("suppressGetSessionWarning" in event.locals.supabase.auth) {
// @ts-expect-error - suppressGetSessionWarning is not part of the official API
event.locals.supabase.auth.suppressGetSessionWarning = true
} else {
console.warn(
"SupabaseAuthClient#suppressGetSessionWarning was removed. See https://github.com/supabase/auth-js/issues/888.",
)
}

/**
* Unlike `supabase.auth.getSession()`, which returns the session _without_
* validating the JWT, this function also calls `getUser()` to validate the
Expand Down
3 changes: 2 additions & 1 deletion src/routes/(admin)/account/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { LayoutServerLoad } from "./$types"

export const load: LayoutServerLoad = async ({
locals: { supabase, safeGetSession },
cookies,
}) => {
const { session, user } = await safeGetSession()

Expand All @@ -16,5 +17,5 @@ export const load: LayoutServerLoad = async ({
.eq("id", user?.id)
.single()

return { session, profile }
return { session, profile, cookies: cookies.getAll() }
}
20 changes: 17 additions & 3 deletions src/routes/(admin)/account/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,31 @@ export const load = async ({ fetch, data, depends, url }) => {
fetch,
},
cookies: {
// TODO: does not match latest supabase guide
get() {
return JSON.stringify(data.session)
getAll() {
return data.cookies
},
},
})

/**
* It's fine to use `getSession` here, because on the client, `getSession` is
* safe, and on the server, it reads `session` from the `LayoutData`, which
* safely checked the session using `safeGetSession`.
* Source: https://supabase.com/docs/guides/auth/server-side/sveltekit
*/
const {
data: { session },
} = await supabase.auth.getSession()

// https://github.com/supabase/auth-js/issues/888#issuecomment-2189298518
if ("suppressGetSessionWarning" in supabase.auth) {
// @ts-expect-error - suppressGetSessionWarning is not part of the official API
supabase.auth.suppressGetSessionWarning = true
} else {
console.warn(
"SupabaseAuthClient#suppressGetSessionWarning was removed. See https://github.com/supabase/auth-js/issues/888.",
)
}
const {
data: { user },
} = await supabase.auth.getUser()
Expand Down
2 changes: 2 additions & 0 deletions src/routes/(marketing)/login/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { LayoutServerLoad } from "./$types"
export const load: LayoutServerLoad = async ({
url,
locals: { safeGetSession },
cookies,
}) => {
const { session } = await safeGetSession()

Expand All @@ -15,5 +16,6 @@ export const load: LayoutServerLoad = async ({
return {
session: session,
url: url.origin,
cookies: cookies.getAll(),
}
}
4 changes: 2 additions & 2 deletions src/routes/(marketing)/login/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export const load = async ({ fetch, data, depends }) => {
fetch,
},
cookies: {
get() {
return JSON.stringify(data.session)
getAll() {
return data.cookies
},
},
})
Expand Down

0 comments on commit c8eb9ed

Please sign in to comment.