Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions svelte-kit-scss/src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@ import { HEADER_NAMES } from '$lib/constants/headers';
import type { Handle, HandleFetch } from '@sveltejs/kit';

export const handle: Handle = async ({ event, resolve }) => {
const accessTokenFromCookies = event.cookies.get(AUTH_COOKIE_NAME);

event.locals.accessToken = accessTokenFromCookies;

if (!accessTokenFromCookies) {
if (!event.url.pathname.startsWith('/signin')) {
return Response.redirect(`${event.url.origin}/signin`, 301);
}
}

if (event.url.pathname.startsWith('/signin')) {
if (accessTokenFromCookies) {
return Response.redirect(`${event.url.origin}`);
}
}

// erase token cookie
if (event.url.pathname === '/logout') {
event.cookies.set(AUTH_COOKIE_NAME, String(), AUTH_COOKIE_ERASE_OPTIONS);
Expand Down
17 changes: 12 additions & 5 deletions svelte-kit-scss/src/routes/(authenticated)/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import type { LayoutServerLoad } from './$types';
import { UserService } from '$lib/services';
import { AUTH_COOKIE_NAME } from '$lib/constants/auth';
import { redirect } from '@sveltejs/kit';

export const load: LayoutServerLoad = async ({ locals, fetch }) => {
const userService = new UserService(fetch);
const authenticatedUser = await userService.getAuthenticatedUser();
locals.user = authenticatedUser;
return authenticatedUser;
export const load: LayoutServerLoad = async ({ locals, fetch, cookies }) => {
const token = cookies.get(AUTH_COOKIE_NAME);
if (token) {
const userService = new UserService(fetch);
const authenticatedUser = await userService.getAuthenticatedUser();
locals.user = authenticatedUser;
return authenticatedUser;
} else {
throw redirect(303, '/signin');
}
};
10 changes: 10 additions & 0 deletions svelte-kit-scss/src/routes/signin/(auth)/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { LayoutServerLoad } from '../../$types';
import { AUTH_COOKIE_NAME } from '$lib/constants/auth';
import { redirect } from '@sveltejs/kit';

export const load: LayoutServerLoad = async ({ cookies }) => {
const token = cookies.get(AUTH_COOKIE_NAME);
if (token) {
throw redirect(303, '/');
}
};