From 011737c52761785b5191b46924d01195c305c3af Mon Sep 17 00:00:00 2001 From: core Date: Mon, 18 Dec 2023 12:33:34 -0500 Subject: [PATCH] frontend magic link, make tokens always end in 'tf' --- tfweb/src/routes/magic-link/+page.server.ts | 54 +++++++++++++++++++++ tfweb/src/routes/magic-link/+page.svelte | 19 ++++++++ trifid-api/src/id.rs | 4 +- 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 tfweb/src/routes/magic-link/+page.server.ts create mode 100644 tfweb/src/routes/magic-link/+page.svelte diff --git a/tfweb/src/routes/magic-link/+page.server.ts b/tfweb/src/routes/magic-link/+page.server.ts new file mode 100644 index 0000000..ac79f87 --- /dev/null +++ b/tfweb/src/routes/magic-link/+page.server.ts @@ -0,0 +1,54 @@ +import type {PageServerLoad} from "./$types"; +import {env} from "$env/dynamic/public"; +import { redirect } from '@sveltejs/kit'; + +export const load: PageServerLoad = async ({cookies, url}) => { + if (url.searchParams.get("magicLinkToken") === null) { + return { + success: false, + error_code: "missing_token", + error_msg: "Missing magic link token" + } + } else { + let resp; + try { + resp = await fetch(`${env.PUBLIC_BASE_URL}/v1/auth/verify-magic-link`, { + method: 'POST', + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + magicLinkToken: url.searchParams.get("magicLinkToken") + }) + }); + } catch (e) { + console.error(e); + return { + success: false, + error_code: "server_error", + error_message: `Failed to contact API: ${e}` + } + } + + let body = await resp.json(); + + if (!resp.ok) { + return { + success: false, + error_code: body.errors[0].code, + error_message: body.errors[0].message + } + } else { + cookies.set("tfsession", body.data.sessionToken, { + maxAge: env.PUBLIC_SESSION_TOKEN_EXPIRY_TIME, + path: '/' + }); + if (cookies.get("tfmfa") !== undefined) { + redirect(301, "/dashboard"); + } else { + redirect(301, "/mfa"); + } + } + + } +} \ No newline at end of file diff --git a/tfweb/src/routes/magic-link/+page.svelte b/tfweb/src/routes/magic-link/+page.svelte new file mode 100644 index 0000000..0c3a48a --- /dev/null +++ b/tfweb/src/routes/magic-link/+page.svelte @@ -0,0 +1,19 @@ + + +
+ + + Logging you in... + + {#if !data.success} + There was an error: {data.error_code} {data.error_message} + {/if} + + + +
\ No newline at end of file diff --git a/trifid-api/src/id.rs b/trifid-api/src/id.rs index 13fc147..c4b7805 100644 --- a/trifid-api/src/id.rs +++ b/trifid-api/src/id.rs @@ -31,10 +31,10 @@ macro_rules! randid { } pub fn random_with_charset(len: u32, charset: &[u8]) -> String { - (0..len) + (0..(len-2)) .map(|_| { let idx = rand::thread_rng().gen_range(0..charset.len()); charset[idx] as char }) - .collect() + .collect::() + "tf" }