diff options
| author | Adam Soltys <[email protected]> | 2022-11-02 19:47:29 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-11-02 19:48:18 -0700 |
| commit | f0d88333750a2865d3016e92620a87269160effa (patch) | |
| tree | 1cc276efeb5287e1fc176ec60d46aed091ab08ca /src/routes/(app)/+layout.server.js | |
| parent | b66477f0912349ef91adaff8c57ac787b5a56447 (diff) | |
| download | coinos-ui-f0d88333750a2865d3016e92620a87269160effa.tar.gz coinos-ui-f0d88333750a2865d3016e92620a87269160effa.zip | |
move to app layout
Diffstat (limited to 'src/routes/(app)/+layout.server.js')
| -rw-r--r-- | src/routes/(app)/+layout.server.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/routes/(app)/+layout.server.js b/src/routes/(app)/+layout.server.js new file mode 100644 index 00000000..6d1fc3e0 --- /dev/null +++ b/src/routes/(app)/+layout.server.js @@ -0,0 +1,42 @@ +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'); + } + + if (['/login', '/register'].includes(url.pathname)) + throw redirect(307, `/${user.username}/dashboard`); + + 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 }; +} |
