From 3d36d7e05229b2a341a8118e45568aee5cf1f603 Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Wed, 22 Feb 2023 11:40:21 -0500 Subject: [PATCH] frontend work for redirecting and auth checks --- tfweb/src/components/Header.svelte | 4 ++-- tfweb/src/components/Theme.svelte | 23 ++++++------------- tfweb/src/lib/auth.ts | 8 +++---- tfweb/src/lib/stores/ThemeStore.ts | 16 +++++++++++-- tfweb/src/routes/admin/+page.svelte | 11 ++++++--- tfweb/src/routes/auth/login/+page.svelte | 12 ++++++++++ tfweb/src/routes/auth/mfa/+page.svelte | 8 +++++++ .../src/routes/v1/auth/check_session.rs | 1 - 8 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 tfweb/src/routes/auth/mfa/+page.svelte diff --git a/tfweb/src/components/Header.svelte b/tfweb/src/components/Header.svelte index 78e9df7..4000b0a 100644 --- a/tfweb/src/components/Header.svelte +++ b/tfweb/src/components/Header.svelte @@ -1,8 +1,8 @@ + \ No newline at end of file diff --git a/tfweb/src/lib/auth.ts b/tfweb/src/lib/auth.ts index bb8dd9e..a61e169 100644 --- a/tfweb/src/lib/auth.ts +++ b/tfweb/src/lib/auth.ts @@ -1,5 +1,4 @@ import {fetch_timeout} from "./util"; -import {t} from "./i18n"; import {API_ROOT} from "./config"; import {Logger, logSetup} from "./logger"; import {getCookie} from "./cookie"; @@ -9,7 +8,7 @@ const logger = new Logger("auth.ts"); export function redact_token(token: string) { const stars = "*".repeat(token.length - 5); - return token.substring(5) + stars; + return token.substring(0, 5) + stars; } export async function enforce_session(): Promise<[boolean, string]> { @@ -31,16 +30,18 @@ export async function enforce_session(): Promise<[boolean, string]> { }); if (!resp.ok) { const rawerror = JSON.parse(await resp.text()).errors[0].message; + logger.error(`session token is invalid: ${rawerror}`); return [false, rawerror]; } else { + logger.info("session token OK"); // session ok return [true, session_token]; } } catch (e) { // error in http request + logger.error(`session token is invalid: ${e}`); return [false, `${e}`] } - return [false, ""]; } export async function enforce_auth(): Promise<[boolean, string]> { @@ -82,5 +83,4 @@ export async function enforce_auth(): Promise<[boolean, string]> { // error in http request return [false, `${e}`] } - return [false, ""]; } \ No newline at end of file diff --git a/tfweb/src/lib/stores/ThemeStore.ts b/tfweb/src/lib/stores/ThemeStore.ts index ffe7975..e00da69 100644 --- a/tfweb/src/lib/stores/ThemeStore.ts +++ b/tfweb/src/lib/stores/ThemeStore.ts @@ -1,3 +1,15 @@ -import { persist } from "$lib/PersistentStore"; +import {writable} from "svelte/store"; +import {browser} from "$app/environment"; -export const theme = persist("theme", "light"); \ No newline at end of file +export const theme = writable(browser && localStorage.getItem("theme") || "light"); +theme.subscribe((value: string) => { + if (browser) { + if (value === "dark") { + localStorage.setItem("theme", "dark"); + document.documentElement.classList.add("dark"); + } else { + localStorage.setItem("theme", "light"); + document.documentElement.classList.remove("dark"); + } + } +}); \ No newline at end of file diff --git a/tfweb/src/routes/admin/+page.svelte b/tfweb/src/routes/admin/+page.svelte index bc9ccb7..a4eee09 100644 --- a/tfweb/src/routes/admin/+page.svelte +++ b/tfweb/src/routes/admin/+page.svelte @@ -1,16 +1,21 @@
diff --git a/tfweb/src/routes/auth/mfa/+page.svelte b/tfweb/src/routes/auth/mfa/+page.svelte new file mode 100644 index 0000000..849ce91 --- /dev/null +++ b/tfweb/src/routes/auth/mfa/+page.svelte @@ -0,0 +1,8 @@ + \ No newline at end of file diff --git a/trifid-api/src/routes/v1/auth/check_session.rs b/trifid-api/src/routes/v1/auth/check_session.rs index 7d78d46..fea33e8 100644 --- a/trifid-api/src/routes/v1/auth/check_session.rs +++ b/trifid-api/src/routes/v1/auth/check_session.rs @@ -6,7 +6,6 @@ pub async fn options() -> &'static str { "" } - #[post("/v1/auth/check_session")] pub async fn check_session(_user: PartialUserInfo) -> &'static str { "ok"