diff options
| author | Adam Soltys <[email protected]> | 2023-01-03 00:22:30 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-01-05 07:41:32 -0800 |
| commit | a657fab89a7de41f0a33389023166a6f7af6dbe6 (patch) | |
| tree | 77a485e5539be24542b38de582338946d317a4ce /src/routes | |
| parent | 9849f6c4431214665c47c43098d86112c93d8001 (diff) | |
| download | coinos-ui-a657fab89a7de41f0a33389023166a6f7af6dbe6.tar.gz coinos-ui-a657fab89a7de41f0a33389023166a6f7af6dbe6.zip | |
generate pubkey on register
Diffstat (limited to 'src/routes')
| -rw-r--r-- | src/routes/(app)/+layout.server.js | 27 | ||||
| -rw-r--r-- | src/routes/(app)/[username]/generate/+page.svelte | 42 | ||||
| -rw-r--r-- | src/routes/(app)/[username]/generate/+server.js | 5 | ||||
| -rw-r--r-- | src/routes/(app)/login/+server.js | 6 | ||||
| -rw-r--r-- | src/routes/(app)/register/+page.server.js | 7 |
5 files changed, 65 insertions, 22 deletions
diff --git a/src/routes/(app)/+layout.server.js b/src/routes/(app)/+layout.server.js index 88ca1539..eef1bdec 100644 --- a/src/routes/(app)/+layout.server.js +++ b/src/routes/(app)/+layout.server.js @@ -1,18 +1,35 @@ import { error, redirect } from '@sveltejs/kit'; -import { get, post, auth } from '$lib/utils'; +import { get, post, auth, protectedRoutes } from '$lib/utils'; import { PUBLIC_BTC as btc } from '$env/static/public'; -export async function load({ cookies, locals, request, url, params }) { - url.pathname; +export async function load({ cookies, request, url, params }) { + let { pathname } = url; let token = cookies.get('token'); let rate, rates = { USD: 1 }, subject; - let { user } = locals; + 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')) + 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; diff --git a/src/routes/(app)/[username]/generate/+page.svelte b/src/routes/(app)/[username]/generate/+page.svelte index f2e53be7..91c3d189 100644 --- a/src/routes/(app)/[username]/generate/+page.svelte +++ b/src/routes/(app)/[username]/generate/+page.svelte @@ -1,14 +1,14 @@ <script> - import { failure } from '$lib/utils'; + import { failure, post, wait } from '$lib/utils'; import { Buffer } from 'buffer'; import { onMount } from 'svelte'; import { bech32m } from 'bech32'; import { generateMnemonic, mnemonicToEntropy, mnemonicToSeedSync } from 'bip39'; import { goto, invalidate } from '$app/navigation'; - import { loginRedirect, password } from '$lib/store'; import { Pin } from '$comp'; - import { pin } from '$lib/store'; + import { loginRedirect, pin, password, passwordPrompt } from '$lib/store'; import { generate } from '$lib/nostr'; + import { browser } from '$app/environment'; export let data; let { user } = data; @@ -16,8 +16,42 @@ let loaded; onMount(async () => { + if (!browser) return; setTimeout(() => (loaded = true), 50); - generate(user); + + if (!$password) $passwordPrompt = true; + + try { + await post('/password', { password: $password }); + } catch (e) { + $passwordPrompt = true; + } + + await wait(() => $password); + + try { + await generate(user); + } catch (e) { + console.log('error generating', e); + } + + try { + user.pin = $pin; + } catch (e) { + if (e.message?.startsWith('Pin')) { + $pin = ''; + failure('Wrong pin, try again'); + } + } + + try { + await post(`/${user.username}/generate`, user); + goto($loginRedirect || `/${user.username}/dashboard`, { invalidateAll: true }); + } catch (e) { + console.log(e); + failure('Failed to generate keys'); + goto('/'); + } }); $: $pin?.length === 6 && generate(user); diff --git a/src/routes/(app)/[username]/generate/+server.js b/src/routes/(app)/[username]/generate/+server.js index 608f0575..4cc248bd 100644 --- a/src/routes/(app)/[username]/generate/+server.js +++ b/src/routes/(app)/[username]/generate/+server.js @@ -3,10 +3,9 @@ import { error, json } from '@sveltejs/kit'; export async function POST({ cookies, request }) { try { - let body = await request.json(); - let user = await post(`/user`, body, auth(cookies)); - return json(user); + return json(await post(`/user`, await request.json(), auth(cookies))); } catch (e) { + console.log(e); throw error(500, e.message); } } diff --git a/src/routes/(app)/login/+server.js b/src/routes/(app)/login/+server.js deleted file mode 100644 index 32d2244d..00000000 --- a/src/routes/(app)/login/+server.js +++ /dev/null @@ -1,6 +0,0 @@ -import { json } from '@sveltejs/kit'; -import { post } from '$lib/utils'; - -export async function POST({ request }) { - return json(await post('/checkPassword', await request.json())); -} diff --git a/src/routes/(app)/register/+page.server.js b/src/routes/(app)/register/+page.server.js index 33cad4d0..793bd7f6 100644 --- a/src/routes/(app)/register/+page.server.js +++ b/src/routes/(app)/register/+page.server.js @@ -9,9 +9,8 @@ export const load = async ({ parent }) => { export const actions = { default: async ({ cookies, request }) => { let form = Object.fromEntries(await request.formData()); - let { username, password } = form; - - let user = { username, password }; + let { username, password, cipher, salt, pubkey } = form; + let user = { username, password, cipher, salt, pubkey }; let error; try { @@ -28,6 +27,6 @@ export const actions = { } if (error) return invalid(400, { error }); - throw redirect(307, `/${user.username}/generate`); + throw redirect(307, `/${user.username}/dashboard`); } }; |
