fix: cache auth session on locals to prevent cookies.set after response
All checks were successful
CI / update (push) Successful in 2m19s
All checks were successful
CI / update (push) Successful in 2m19s
The authorization hook already calls locals.auth() which can set cookies. Layout server loads calling auth() again caused a race where cookies.set() fired after the response started streaming. Now the hook stashes the session on locals.session and all layouts reuse it.
This commit is contained in:
1
src/app.d.ts
vendored
1
src/app.d.ts
vendored
@@ -12,6 +12,7 @@ declare global {
|
|||||||
}
|
}
|
||||||
interface Locals {
|
interface Locals {
|
||||||
auth(): Promise<Session | null>;
|
auth(): Promise<Session | null>;
|
||||||
|
session?: Session | null;
|
||||||
}
|
}
|
||||||
// interface PageData {}
|
// interface PageData {}
|
||||||
interface PageState {
|
interface PageState {
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ await dbConnect().then(() => {
|
|||||||
|
|
||||||
async function authorization({ event, resolve }: Parameters<Handle>[0]) {
|
async function authorization({ event, resolve }: Parameters<Handle>[0]) {
|
||||||
const session = await event.locals.auth();
|
const session = await event.locals.auth();
|
||||||
|
event.locals.session = session;
|
||||||
|
|
||||||
// Protect rezepte routes
|
// Protect rezepte routes
|
||||||
if (event.url.pathname.startsWith('/rezepte/edit') || event.url.pathname.startsWith('/rezepte/add')) {
|
if (event.url.pathname.startsWith('/rezepte/edit') || event.url.pathname.startsWith('/rezepte/add')) {
|
||||||
if (!session) {
|
if (!session) {
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ import type { LayoutServerLoad } from "./$types"
|
|||||||
|
|
||||||
export const load : LayoutServerLoad = (async ({locals}) => {
|
export const load : LayoutServerLoad = (async ({locals}) => {
|
||||||
return {
|
return {
|
||||||
session: await locals.auth(),
|
session: locals.session ?? await locals.auth(),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export const load : LayoutServerLoad = async ({locals, params}) => {
|
|||||||
const lang = params.faithLang === 'faith' ? 'en' : 'de';
|
const lang = params.faithLang === 'faith' ? 'en' : 'de';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
session: await locals.auth(),
|
session: locals.session ?? await locals.auth(),
|
||||||
lang,
|
lang,
|
||||||
faithLang: params.faithLang
|
faithLang: params.faithLang
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export const load : LayoutServerLoad = async ({locals, params}) => {
|
|||||||
const lang = params.recipeLang === 'recipes' ? 'en' : 'de';
|
const lang = params.recipeLang === 'recipes' ? 'en' : 'de';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
session: await locals.auth(),
|
session: locals.session ?? await locals.auth(),
|
||||||
lang,
|
lang,
|
||||||
recipeLang: params.recipeLang
|
recipeLang: params.recipeLang
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ import type { LayoutServerLoad } from "./$types"
|
|||||||
|
|
||||||
export const load : LayoutServerLoad = async ({locals}) => {
|
export const load : LayoutServerLoad = async ({locals}) => {
|
||||||
return {
|
return {
|
||||||
session: await locals.auth()
|
session: locals.session ?? await locals.auth()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ import type { LayoutServerLoad } from './$types';
|
|||||||
|
|
||||||
export const load: LayoutServerLoad = async ({ locals }) => {
|
export const load: LayoutServerLoad = async ({ locals }) => {
|
||||||
return {
|
return {
|
||||||
session: await locals.auth()
|
session: locals.session ?? await locals.auth()
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user