aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/+layout.server.js
blob: 6af13a06400288f75e2c3d41cfdd145b790bb7d1 (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
import { error, redirect } from '@sveltejs/kit';
import { protectedRoutes } from '$lib/utils';
import { get, post, auth } from '$lib/utils';
import { env } from '$env/dynamic/public';

export const { PUBLIC_BTC: btc } = env;

export async function load({ cookies, locals, request, url, params }) {
	url.pathname;
	let token = cookies.get('token');
	let user;
	let rates;
	let subject;

	if (params.username) {
		subject = await get(`/users/${params.username}`);
	}

	if (token && token !== 'undefined') {
		user = locals.user;

		if (protectedRoutes.find((p) => url.pathname.match(p))) {
			if (user.username !== params.username) throw error(404, 'user not found');
		}

		let account = user.accounts.find((a) => a.asset === btc && !a.pubkey);
		if (account) {
			let { id } = account;
			if (user.account_id !== id) {
				user = await post('/shiftAccount', { id }, auth(cookies));
			}
		}
	}

	let rate = await get('/rate');
	rates = await get('/rates');

	return { subject, user, token, rate, rates };
}