Skip to content

Commit f6cd906

Browse files
committed
new auth flow using sequence
1 parent 8957871 commit f6cd906

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

src/hooks.server.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
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';
33
import { 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);
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
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>
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
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>

src/routes/(dashboard)/+layout.server.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
export const ssr = false;
22

3-
import { redirect } from '@sveltejs/kit';
43
import type { LayoutServerLoad } from './$types';
54

65
export const load: LayoutServerLoad = async ({ locals }) => {
76
const { userId } = locals.auth();
87

9-
if (!userId) return redirect(307, '/login');
10-
118
return {
129
userId
1310
};

src/routes/(dashboard)/+layout.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
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" />

0 commit comments

Comments
 (0)