frontend work for redirecting and auth checks
This commit is contained in:
parent
33a0c077d1
commit
3d36d7e052
|
@ -1,8 +1,8 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {t} from "$lib/i18n";
|
import {t} from "$lib/i18n";
|
||||||
import {theme} from "$lib/stores/ThemeStore.js";
|
|
||||||
import {getCurrentLocale, locales} from "$lib/i18n.js";
|
import {getCurrentLocale, locales} from "$lib/i18n.js";
|
||||||
import {locale} from "$lib/stores/LocaleStore";
|
import {locale} from "$lib/stores/LocaleStore";
|
||||||
|
import {theme} from "$lib/stores/ThemeStore";
|
||||||
|
|
||||||
function toggleTheme() {
|
function toggleTheme() {
|
||||||
if ($theme === "dark") {
|
if ($theme === "dark") {
|
||||||
|
@ -73,7 +73,7 @@
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{#if $theme === "dark"}
|
{#if $theme === 'dark'}
|
||||||
<button title="{t('header.lightMode')}" class="inline-block text-sm px-4 leading-none mt-4 lg:mt-0"
|
<button title="{t('header.lightMode')}" class="inline-block text-sm px-4 leading-none mt-4 lg:mt-0"
|
||||||
on:click={toggleTheme}>
|
on:click={toggleTheme}>
|
||||||
<i class="fa-solid fa-sun"></i>
|
<i class="fa-solid fa-sun"></i>
|
||||||
|
|
|
@ -1,24 +1,15 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {onMount} from "svelte";
|
|
||||||
import {Logger, logSetup} from "$lib/logger";
|
|
||||||
import {theme} from "$lib/stores/ThemeStore";
|
|
||||||
import {get} from "svelte/store";
|
|
||||||
|
|
||||||
onMount(() => {
|
|
||||||
let logger = new Logger("Theme.svelte");
|
|
||||||
logSetup();
|
|
||||||
|
|
||||||
theme.subscribe((newTheme) => {
|
|
||||||
if (newTheme === "dark") {
|
|
||||||
document.documentElement.classList.add("dark");
|
|
||||||
} else {
|
|
||||||
document.documentElement.classList.remove("dark");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
<link rel="stylesheet" href="https://cdn.e3t.cc/fa/6.2.0/css/all.min.css"/>
|
<link rel="stylesheet" href="https://cdn.e3t.cc/fa/6.2.0/css/all.min.css"/>
|
||||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lipis/flag-icons@6.6.6/css/flag-icons.min.css"/>
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lipis/flag-icons@6.6.6/css/flag-icons.min.css"/>
|
||||||
|
<script>
|
||||||
|
if (localStorage.theme === "dark") {
|
||||||
|
document.documentElement.classList.add("dark");
|
||||||
|
} else {
|
||||||
|
document.documentElement.classList.remove("dark");
|
||||||
|
}
|
||||||
|
</script>
|
||||||
</svelte:head>
|
</svelte:head>
|
|
@ -1,5 +1,4 @@
|
||||||
import {fetch_timeout} from "./util";
|
import {fetch_timeout} from "./util";
|
||||||
import {t} from "./i18n";
|
|
||||||
import {API_ROOT} from "./config";
|
import {API_ROOT} from "./config";
|
||||||
import {Logger, logSetup} from "./logger";
|
import {Logger, logSetup} from "./logger";
|
||||||
import {getCookie} from "./cookie";
|
import {getCookie} from "./cookie";
|
||||||
|
@ -9,7 +8,7 @@ const logger = new Logger("auth.ts");
|
||||||
|
|
||||||
export function redact_token(token: string) {
|
export function redact_token(token: string) {
|
||||||
const stars = "*".repeat(token.length - 5);
|
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]> {
|
export async function enforce_session(): Promise<[boolean, string]> {
|
||||||
|
@ -31,16 +30,18 @@ export async function enforce_session(): Promise<[boolean, string]> {
|
||||||
});
|
});
|
||||||
if (!resp.ok) {
|
if (!resp.ok) {
|
||||||
const rawerror = JSON.parse(await resp.text()).errors[0].message;
|
const rawerror = JSON.parse(await resp.text()).errors[0].message;
|
||||||
|
logger.error(`session token is invalid: ${rawerror}`);
|
||||||
return [false, rawerror];
|
return [false, rawerror];
|
||||||
} else {
|
} else {
|
||||||
|
logger.info("session token OK");
|
||||||
// session ok
|
// session ok
|
||||||
return [true, session_token];
|
return [true, session_token];
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// error in http request
|
// error in http request
|
||||||
|
logger.error(`session token is invalid: ${e}`);
|
||||||
return [false, `${e}`]
|
return [false, `${e}`]
|
||||||
}
|
}
|
||||||
return [false, ""];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function enforce_auth(): Promise<[boolean, string]> {
|
export async function enforce_auth(): Promise<[boolean, string]> {
|
||||||
|
@ -82,5 +83,4 @@ export async function enforce_auth(): Promise<[boolean, string]> {
|
||||||
// error in http request
|
// error in http request
|
||||||
return [false, `${e}`]
|
return [false, `${e}`]
|
||||||
}
|
}
|
||||||
return [false, ""];
|
|
||||||
}
|
}
|
|
@ -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");
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
|
@ -1,16 +1,21 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {onMount} from "svelte";
|
import {onMount} from "svelte";
|
||||||
import {enforce_auth, enforce_session} from "../../lib/auth";
|
import {enforce_auth, enforce_session} from "../../lib/auth";
|
||||||
|
import {Logger, logSetup} from "../../lib/logger";
|
||||||
|
|
||||||
|
let logger = new Logger("admin/+page.svelte");
|
||||||
|
logSetup();
|
||||||
|
|
||||||
// this page requires session and mfa auth.
|
// this page requires session and mfa auth.
|
||||||
onMount(() => {
|
onMount(async () => {
|
||||||
let st_result = enforce_session();
|
let st_result = await enforce_session();
|
||||||
if (!st_result[0]) {
|
if (!st_result[0]) {
|
||||||
|
logger.info(st_result);
|
||||||
// Session token is invalid. redirect to login
|
// Session token is invalid. redirect to login
|
||||||
window.location = "/auth/login";
|
window.location = "/auth/login";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let at_result = enforce_auth();
|
let at_result = await enforce_auth();
|
||||||
if (!at_result[0]) {
|
if (!at_result[0]) {
|
||||||
// Auth token is invalid. Redirect to mfa page.
|
// Auth token is invalid. Redirect to mfa page.
|
||||||
window.location = "/auth/mfa";
|
window.location = "/auth/mfa";
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
import {API_ROOT} from "$lib/config";
|
import {API_ROOT} from "$lib/config";
|
||||||
import {fetch_timeout} from "$lib/util";
|
import {fetch_timeout} from "$lib/util";
|
||||||
import {Logger, logSetup} from "../../../lib/logger";
|
import {Logger, logSetup} from "../../../lib/logger";
|
||||||
|
import {onMount} from "svelte";
|
||||||
|
import {enforce_session} from "$lib/auth";
|
||||||
|
|
||||||
let email = "";
|
let email = "";
|
||||||
let isloading = false;
|
let isloading = false;
|
||||||
|
@ -48,6 +50,16 @@
|
||||||
isloading = false;
|
isloading = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onMount(async () => {
|
||||||
|
let st_result = await enforce_session();
|
||||||
|
if (st_result[0]) {
|
||||||
|
// User already authed, redirect them
|
||||||
|
logger.info("User already logged in");
|
||||||
|
window.location.href = "/admin";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="flex in-h-full items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
|
<div class="flex in-h-full items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
<script lang="ts">
|
||||||
|
import {onMount} from "svelte";
|
||||||
|
import {enforce_session} from "../../../lib/auth";
|
||||||
|
|
||||||
|
onMount(async () => {
|
||||||
|
let st_res = await enforce_session();
|
||||||
|
});
|
||||||
|
</script>
|
|
@ -6,7 +6,6 @@ pub async fn options() -> &'static str {
|
||||||
""
|
""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[post("/v1/auth/check_session")]
|
#[post("/v1/auth/check_session")]
|
||||||
pub async fn check_session(_user: PartialUserInfo) -> &'static str {
|
pub async fn check_session(_user: PartialUserInfo) -> &'static str {
|
||||||
"ok"
|
"ok"
|
||||||
|
|
Loading…
Reference in New Issue