aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/+layout.server.js
blob: eef1bdecea82bdf750a4fcb130697a9899ea8946 (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
42
43
44
45
46
47
48
import { error, redirect } from '@sveltejs/kit';
import { get, post, auth, protectedRoutes } from '$lib/utils';
import { PUBLIC_BTC as btc } from '$env/static/public';

export async function load({ cookies, request, url, params }) {
	let { pathname } = url;
	let token = cookies.get('token');
	let rate,
		rates = { USD: 1 },
		subject;

	let user;
	if (token) {
		try {
			user = await get('/me', auth(cookies));
		} catch (e) {}
	}

	if (user && ['/', '/login', '/register'].includes(pathname) && request.method === 'GET')
		throw redirect(307, `/${user.username}/dashboard`);

	if (
		protectedRoutes.find((p) => pathname.match(p)) &&
		(!user || user.username !== params.username)
	) {
		throw redirect(307, '/login');
	}

	if (user) {
		if (!user.pubkey && !url.pathname.includes('gen')) {
			throw redirect(307, `/${user.username}/generate`);
		}
		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));
			}
		}
	}

	try {
		rate = await get('/rate');
		rates = await get('/rates');
	} catch (e) {}

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