Skip to content

Commit 956528d

Browse files
fix: lucia require user redirect moved to auth guard function (#558)
* fix: lucia require user redirect moved to auth guard function * Create clever-chicken-compete.md --------- Co-authored-by: Manuel <[email protected]>
1 parent ca07fcd commit 956528d

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

.changeset/clever-chicken-compete.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"sv": patch
3+
---
4+
5+
fix: lucia require user redirect moved to auth guard function

packages/addons/lucia/index.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,11 @@ export default defineAddon({
561561
return dedent`
562562
import * as auth from '$lib/server/auth';
563563
import { fail, redirect } from '@sveltejs/kit';
564+
import { getRequestEvent } from '$app/server';
564565
${ts("import type { Actions, PageServerLoad } from './$types';\n")}
565-
export const load${ts(': PageServerLoad')} = async (event) => {
566-
if (!event.locals.user) {
567-
return redirect(302, '/demo/lucia/login');
568-
}
569-
return { user: event.locals.user };
566+
export const load${ts(': PageServerLoad')} = async () => {
567+
const user = requireLogin()
568+
return { user };
570569
};
571570
572571
export const actions${ts(': Actions')} = {
@@ -580,6 +579,16 @@ export default defineAddon({
580579
return redirect(302, '/demo/lucia/login');
581580
},
582581
};
582+
583+
function requireLogin() {
584+
const { locals } = getRequestEvent();
585+
586+
if (!locals.user) {
587+
return redirect(302, "/demo/lucia/login");
588+
}
589+
590+
return locals.user;
591+
}
583592
`;
584593
});
585594

0 commit comments

Comments
 (0)