diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-12-13 14:48:11 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-12-13 14:48:21 +0100 |
| commit | a8219611cbebbd27501d9f30c804979048b98107 (patch) | |
| tree | 632dbab8b724f0148b06525771d85ad6ddb55e35 /code/app/src/routes/(main)/+layout.server.ts | |
| parent | db2d937a38d5c309e3c6aa14f2eaf3cfe58f415e (diff) | |
| download | greatoffice-a8219611cbebbd27501d9f30c804979048b98107.tar.xz greatoffice-a8219611cbebbd27501d9f30c804979048b98107.zip | |
feat: A whole slew of things
- Use a md5 hash of the session cookie value as key for session validity check
- Introduce global state
- Introduce a common interface for form logic, and implement it on the sign-in form
- Introduce static resolve() on all services instead of new-upping all over.
- Implement /portal on the frontend to support giving the frontend a inital context from server or anywhere.
- Show a notification when users sign in for the first time after validating their email
Diffstat (limited to 'code/app/src/routes/(main)/+layout.server.ts')
| -rw-r--r-- | code/app/src/routes/(main)/+layout.server.ts | 74 |
1 files changed, 27 insertions, 47 deletions
diff --git a/code/app/src/routes/(main)/+layout.server.ts b/code/app/src/routes/(main)/+layout.server.ts index 4199d7f..b040b8f 100644 --- a/code/app/src/routes/(main)/+layout.server.ts +++ b/code/app/src/routes/(main)/+layout.server.ts @@ -1,35 +1,41 @@ -import {api_base, CookieNames} from "$configuration"; -import {log_debug, log_error} from "$help/logger"; -import {error, redirect} from "@sveltejs/kit"; -import {Temporal} from "temporal-polyfill"; -import type {LayoutServerLoad} from "./$types"; +import { api_base, CookieNames } from "$configuration"; +import { cached_result_async, CacheKeys } from "$help/cache"; +import { log_debug, log_error } from "$help/logger"; +import { md5 } from "$help/md5"; +import { error, redirect } from "@sveltejs/kit"; +import type { LayoutServerLoad } from "./$types"; -export const load: LayoutServerLoad = async ({url, request, route, cookies, locals, fetch}) => { - console.log(url.toString()); +export const load: LayoutServerLoad = async ({ route, cookies, locals, fetch }) => { const isBaseRoute = route.id === "/(main)"; - const isPublicRoute = (route.id?.startsWith("/(main)/(public)") || isBaseRoute) ?? true; + const isPortalRoute = route.id === "/(main)/(public)/portal"; + const isPublicRoute = (isBaseRoute || (route.id?.startsWith("/(main)/(public)") ?? false)) ?? true; const sessionCookieValue = cookies.get(CookieNames.session); - const hasSessionCookie = (sessionCookieValue?.length > 0 ?? false); - const sessionIsValid = hasSessionCookie && (await cached_result_async<Response>("sessionCheck", 120, () => fetch(api_base("_/is-authenticated"), { - headers: { - Cookie: CookieNames.session + "=" + sessionCookieValue, - }, - }).catch((e) => { - log_error(e); - throw error(503, { - message: "We are experiencing a service disruption! Have patience while we resolve the issue.", - }); - }))).ok; + let sessionIsValid = false; + if ((sessionCookieValue?.length > 0 ?? false)) { + const sessionHash = md5(sessionCookieValue); + sessionIsValid = (await cached_result_async<Response>(sessionHash + "_" + CacheKeys.isAuthenticated, 120, () => fetch(api_base("_/is-authenticated"), { + headers: { + Cookie: CookieNames.session + "=" + sessionCookieValue, + }, + }).catch((e) => { + log_error(e); + throw error(503, { + message: "We are experiencing a service disruption! Have patience while we resolve the issue.", + }); + }))).ok; + } log_debug("Base Layout loaded", { sessionIsValid, isPublicRoute, + isBaseRoute, + isPortalRoute, routeId: route.id, }); - if (sessionIsValid && isPublicRoute) { + if (sessionIsValid && isPublicRoute && !isPortalRoute) { throw redirect(302, "/home"); - } else if (isBaseRoute || !sessionIsValid && !isPublicRoute) { + } else if (!isPortalRoute && (isBaseRoute || !sessionIsValid && !isPublicRoute)) { throw redirect(302, "/sign-in"); } @@ -37,29 +43,3 @@ export const load: LayoutServerLoad = async ({url, request, route, cookies, loca locale: locals.locale, }; }; - -let resultCache = {}; - -async function cached_result_async<T>(key: string, staleAfterSeconds: number, get_result: any, forceRefresh: boolean = false) { - if (!resultCache[key]) { - resultCache[key] = { - l: 0, - c: undefined as T, - }; - } - const staleEpoch = ((resultCache[key]?.l ?? 0) + staleAfterSeconds); - const isStale = forceRefresh || (staleEpoch < Temporal.Now.instant().epochSeconds); - if (isStale || !resultCache[key]?.c) { - resultCache[key].c = await get_result(); - resultCache[key].l = Temporal.Now.instant().epochSeconds; - } - - log_debug("Ran cached_result_async", { - cacheKey: key, - isStale, - cache: resultCache[key], - staleEpoch, - }); - - return resultCache[key].c as T; -}
\ No newline at end of file |
