Skip to content

Commit 80d43fa

Browse files
committed
fix: Sveltekit login locally
1 parent 296f472 commit 80d43fa

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

svelte-kit-scss/src/hooks.server.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@ import { HEADER_NAMES } from '$lib/constants/headers';
44
import type { Handle, HandleFetch } from '@sveltejs/kit';
55

66
export const handle: Handle = async ({ event, resolve }) => {
7-
const accessTokenFromCookies = event.cookies.get(AUTH_COOKIE_NAME);
8-
9-
event.locals.accessToken = accessTokenFromCookies;
10-
11-
if (!accessTokenFromCookies) {
12-
if (!event.url.pathname.startsWith('/signin')) {
13-
return Response.redirect(`${event.url.origin}/signin`, 301);
14-
}
15-
}
16-
17-
if (event.url.pathname.startsWith('/signin')) {
18-
if (accessTokenFromCookies) {
19-
return Response.redirect(`${event.url.origin}`);
20-
}
21-
}
22-
237
// erase token cookie
248
if (event.url.pathname === '/logout') {
259
event.cookies.set(AUTH_COOKIE_NAME, String(), AUTH_COOKIE_ERASE_OPTIONS);
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import type { LayoutServerLoad } from './$types';
22
import { UserService } from '$lib/services';
3+
import { AUTH_COOKIE_NAME } from '$lib/constants/auth';
4+
import { redirect } from '@sveltejs/kit';
35

4-
export const load: LayoutServerLoad = async ({ locals, fetch }) => {
5-
const userService = new UserService(fetch);
6-
const authenticatedUser = await userService.getAuthenticatedUser();
7-
locals.user = authenticatedUser;
8-
return authenticatedUser;
6+
export const load: LayoutServerLoad = async ({ locals, fetch, cookies }) => {
7+
const token = cookies.get(AUTH_COOKIE_NAME);
8+
if (token) {
9+
const userService = new UserService(fetch);
10+
const authenticatedUser = await userService.getAuthenticatedUser();
11+
locals.user = authenticatedUser;
12+
return authenticatedUser;
13+
} else {
14+
throw redirect(303, '/signin');
15+
}
916
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { LayoutServerLoad } from '../../$types';
2+
import { AUTH_COOKIE_NAME } from '$lib/constants/auth';
3+
import { redirect } from '@sveltejs/kit';
4+
5+
export const load: LayoutServerLoad = async ({ cookies }) => {
6+
const token = cookies.get(AUTH_COOKIE_NAME);
7+
if (token) {
8+
throw redirect(303, '/');
9+
}
10+
};

0 commit comments

Comments
 (0)