aboutsummaryrefslogtreecommitdiff
path: root/src/hooks.server.ts
blob: f1f5b5294d3b887014007c79c01bd74e1dd543e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

import { redirect } from '@sveltejs/kit';
import type { Handle } from '@sveltejs/kit';
import { renewSession } from '$lib/server/auth';

const PUBLIC_ASSETS = new Set([
	'/favicon.png',
	'/manifest.webmanifest',
	'/pwa-192x192.png',
	'/pwa-512x512.png',
	'/registerSW.js',
	'/sw.js',
]);

function isPublicPath(pathname: string): boolean {
	return (
		pathname === '/login' ||
		pathname.startsWith('/_app/') ||
		PUBLIC_ASSETS.has(pathname)
	);
}

export const handle: Handle = async ({ event, resolve }) => {
	if (isPublicPath(event.url.pathname)) return resolve(event);

	const secure = event.url.protocol === 'https:';
	if (renewSession(event.cookies, secure)) return resolve(event);

	const next = `${event.url.pathname}${event.url.search}`;
	throw redirect(303, `/login?next=${encodeURIComponent(next)}`);
};