aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/+layout.server.js
blob: 2d95a899387bd0c575470e910c3f1052cad6215b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { error } from '@sveltejs/kit';
import cookie from 'cookie';
import { protectedRoutes } from '$lib/utils';
import { get } from '$lib/utils';

export async function load({ request, setHeaders, url, params }) {
	let { token } = cookie.parse(request.headers.get('cookie') || '');
	let user;
	let rates;

	if (token) {
		user = await get('/me', { accept: 'application/json', authorization: `Bearer ${token}` });
		if (protectedRoutes.find((p) => url.pathname.match(p))) {
			if (user.username !== params.username) throw error(404, 'user not found');
		}
	}

	rates = await get('/rates');

	return { user, token, rates };
}