aboutsummaryrefslogtreecommitdiff
path: root/src/hooks.server.ts
blob: d0f09b6ef211c47818ff5f9455dd018be2d42f20 (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
31
32
33
34
35
36
37
38
39
40
41
import root from '$lib/Utility/root';
import type { Handle, RequestEvent } from '@sveltejs/kit';

const redirectWithParameters = (
	event: RequestEvent<Partial<Record<string, string>>, string | null>,
	path: string
) => {
	return Response.redirect(
		root(
			`${path}${
				event.url.searchParams.toString().length > 0 ? `?${event.url.searchParams.toString()}` : ''
			}`
		),
		307
	);
};

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

	if (user) {
		const parsedUser = JSON.parse(user);

		event.locals.user = {
			tokenType: parsedUser['token_type'],
			expiresIn: parsedUser['expires_in'],
			accessToken: parsedUser['access_token'],
			refreshToken: parsedUser['refresh_token']
		};
	}

	switch (event.url.pathname) {
		case '/birthdays':
			return redirectWithParameters(event, '/tools/birthdays');
		case '/wrapped':
			return redirectWithParameters(event, '/tools/wrapped');
	}

	return await resolve(event);
};