File tree Expand file tree Collapse file tree 5 files changed +37
-28
lines changed Expand file tree Collapse file tree 5 files changed +37
-28
lines changed Original file line number Diff line number Diff line change 1- import { withClerkHandler } from 'svelte-clerk/server ' ;
2- import { PUBLIC_CLERK_PUBLISHABLE_KEY } from '$env/static/public ' ;
1+ import { redirect , type Handle } from '@sveltejs/kit ' ;
2+ import { sequence } from '@sveltejs/kit/hooks ' ;
33import { CLERK_SECRET_KEY } from '$env/static/private' ;
4+ import { PUBLIC_CLERK_PUBLISHABLE_KEY } from '$env/static/public' ;
5+ import { withClerkHandler } from 'svelte-clerk/server' ;
6+
7+ const PROTECTED_ROUTE_PREFIXES = [ '/(dashboard)' ] as const ;
48
5- export const handle = withClerkHandler ( {
9+ const clerkHandle = withClerkHandler ( {
610 publishableKey : PUBLIC_CLERK_PUBLISHABLE_KEY ,
7- secretKey : CLERK_SECRET_KEY
11+ secretKey : CLERK_SECRET_KEY ,
12+ signInUrl : '/login'
813} ) ;
14+
15+ const authRedirectHandle : Handle = async ( { event, resolve } ) => {
16+ const routeId = event . route . id ;
17+ const shouldProtect =
18+ routeId && PROTECTED_ROUTE_PREFIXES . some ( ( prefix ) => routeId . startsWith ( prefix ) ) ;
19+
20+ if ( shouldProtect ) {
21+ const auth = event . locals . auth ;
22+ const { userId } = auth ( ) ;
23+
24+ if ( ! userId ) {
25+ throw redirect ( 302 , '/login' ) ;
26+ }
27+ }
28+
29+ return resolve ( event ) ;
30+ } ;
31+
32+ export const handle = sequence ( clerkHandle , authRedirectHandle ) ;
Original file line number Diff line number Diff line change 11<script lang =" ts" >
2- import Redirect from ' @/lib/components/Redirect.svelte' ;
3- import { SignedIn , SignedOut , SignIn } from ' svelte-clerk/client' ;
2+ import { SignIn } from ' svelte-clerk/client' ;
43 </script >
54
6- <SignedOut >
7- <div class =" flex items-center justify-center py-10" >
8- <SignIn redirectUrl =" /dashboard" />
9- </div >
10- </SignedOut >
11- <SignedIn >
12- <Redirect route =" dashboard" />
13- </SignedIn >
5+ <div class =" flex items-center justify-center py-10" >
6+ <SignIn redirectUrl =" /dashboard" />
7+ </div >
Original file line number Diff line number Diff line change 11<script lang =" ts" >
2- import Redirect from ' @/lib/components/Redirect.svelte' ;
3- import { SignedIn , SignedOut , SignUp } from ' svelte-clerk/client' ;
2+ import { SignUp } from ' svelte-clerk/client' ;
43 </script >
54
6- <SignedOut >
7- <div class =" flex items-center justify-center py-10" >
8- <SignUp redirectUrl =" /dashboard" />
9- </div >
10- </SignedOut >
11- <SignedIn >
12- <Redirect route =" dashboard" />
13- </SignedIn >
5+ <div class =" flex items-center justify-center py-10" >
6+ <SignUp redirectUrl =" /dashboard" />
7+ </div >
Original file line number Diff line number Diff line change 11export const ssr = false ;
22
3- import { redirect } from '@sveltejs/kit' ;
43import type { LayoutServerLoad } from './$types' ;
54
65export const load : LayoutServerLoad = async ( { locals } ) => {
76 const { userId } = locals . auth ( ) ;
87
9- if ( ! userId ) return redirect ( 307 , '/login' ) ;
10-
118 return {
129 userId
1310 } ;
Original file line number Diff line number Diff line change 1818 <AppSidebar user ={user ?? undefined } />
1919 <Sidebar .Inset >
2020 <header
21- class =" flex h-16 shrink-0 items-center gap-2 transition-[width,height] ease-linear group-has-[[ data-collapsible=icon] ]/sidebar-wrapper:h-12"
21+ class =" flex h-16 shrink-0 items-center gap-2 transition-[width,height] ease-linear group-has-data-[ collapsible=icon]/sidebar-wrapper:h-12"
2222 >
2323 <div class =" flex items-center gap-2 px-4" >
2424 <Sidebar .Trigger class =" -ml-1" />
You can’t perform that action at this time.
0 commit comments