From a8219611cbebbd27501d9f30c804979048b98107 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Tue, 13 Dec 2022 14:48:11 +0100 Subject: 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 --- code/app/src/routes/(main)/(app)/+layout.svelte | 27 +++- .../src/routes/(main)/(app)/projects/+page.svelte | 171 +++++++++------------ .../src/routes/(main)/(public)/portal/+page.svelte | 26 ++++ .../app/src/routes/(main)/(public)/portal/+page.ts | 10 ++ .../(main)/(public)/reset-password/+page.svelte | 11 +- .../(public)/reset-password/[id]/+page.svelte | 41 ++--- .../routes/(main)/(public)/sign-in/+page.svelte | 106 ++++++------- code/app/src/routes/(main)/+layout.server.ts | 76 ++++----- 8 files changed, 240 insertions(+), 228 deletions(-) create mode 100644 code/app/src/routes/(main)/(public)/portal/+page.svelte create mode 100644 code/app/src/routes/(main)/(public)/portal/+page.ts (limited to 'code/app/src/routes/(main)') diff --git a/code/app/src/routes/(main)/(app)/+layout.svelte b/code/app/src/routes/(main)/(app)/+layout.svelte index e57bc3b..936e0a7 100644 --- a/code/app/src/routes/(main)/(app)/+layout.svelte +++ b/code/app/src/routes/(main)/(app)/+layout.svelte @@ -22,12 +22,13 @@ TransitionRoot, } from "@rgossiaux/svelte-headlessui"; import { DialogPanel } from "@developermuch/dev-svelte-headlessui"; - import { Input } from "$components"; + import { Input, Notification } from "$components"; import { goto } from "$app/navigation"; import { page } from "$app/stores"; + import { onMount } from "svelte"; + import { fgs, sgs } from "$help/global-state"; - const accountService = new AccountService(); - + const accountService = AccountService.resolve(); const session = { profile: { username: "Brukernavn", @@ -37,6 +38,12 @@ let sidebarOpen = false; let sidebarSearchValue: string | undefined; + let showEmailValidatedNotif = false; + + onMount(() => { + showEmailValidatedNotif = fgs("showEmailValidatedAlertWhenLoggedIn") === "true"; + if (showEmailValidatedNotif) sgs("showEmailValidatedAlertWhenLoggedIn", false); + }); function sign_out() { accountService.end_session(() => goto("/sign-in")); @@ -71,6 +78,20 @@ ]; +{#if showEmailValidatedNotif} + + + +{/if} +
diff --git a/code/app/src/routes/(main)/(app)/projects/+page.svelte b/code/app/src/routes/(main)/(app)/projects/+page.svelte index 1508118..2585331 100644 --- a/code/app/src/routes/(main)/(app)/projects/+page.svelte +++ b/code/app/src/routes/(main)/(app)/projects/+page.svelte @@ -1,41 +1,14 @@
@@ -66,78 +39,80 @@

A list of all the projects in your organsation.

- -
- {#each $headerRows as headerRow (headerRow.id)} - - - {#each headerRow.cells as cell (cell.id)} - - + {#each headerRow.cells as cell (cell.id)} + + - - {/each} - - - {/each} + > + {#if props.sort.order === "asc"} + + {:else if props.sort.order === "desc"} + + {:else if !props.sort.disabled} + + {/if} + + {#if cell.id === "status"} + + {/if} + + + + {/each} + + + {/each} - {#each $rows as row (row.id)} - - - {#each row.cells as cell (cell.id)} - {@const materialisedCell = cell.render()} - - - - {/each} - - - {/each} + {#each $rows as row (row.id)} + + + {#each row.cells as cell (cell.id)} + {@const materialisedCell = cell.render()} + + + + {/each} + + + {/each}
+
-
- - +
+ + - {#if props.sort.order === "asc"} - - {:else if props.sort.order === "desc"} - - {:else if !props.sort.disabled} - - {/if} - - {#if cell.id === "status"} - - {/if} -
-
- {#if cell.id === "name"} - - - - {:else if cell.id === "status"} - - {:else} - - {/if} -
+ {#if cell.id === "name"} + + + + {:else if cell.id === "status"} + + {:else} + + {/if} +
diff --git a/code/app/src/routes/(main)/(public)/portal/+page.svelte b/code/app/src/routes/(main)/(public)/portal/+page.svelte new file mode 100644 index 0000000..bd6aa15 --- /dev/null +++ b/code/app/src/routes/(main)/(public)/portal/+page.svelte @@ -0,0 +1,26 @@ + + +
+

Warping...

+
diff --git a/code/app/src/routes/(main)/(public)/portal/+page.ts b/code/app/src/routes/(main)/(public)/portal/+page.ts new file mode 100644 index 0000000..49bf3db --- /dev/null +++ b/code/app/src/routes/(main)/(public)/portal/+page.ts @@ -0,0 +1,10 @@ +import type { PortalMessage } from '$configuration'; +import { redirect } from '@sveltejs/kit'; +import type { PageLoad } from './$types'; + +export const load: PageLoad = async ({ url }) => { + const queryParams = new URLSearchParams(url.search); + const message = queryParams.get("msg") as PortalMessage; + if (!message) throw redirect(302, "/"); + return { message }; +}; \ No newline at end of file diff --git a/code/app/src/routes/(main)/(public)/reset-password/+page.svelte b/code/app/src/routes/(main)/(public)/reset-password/+page.svelte index 55859f6..a45ccdd 100644 --- a/code/app/src/routes/(main)/(public)/reset-password/+page.svelte +++ b/code/app/src/routes/(main)/(public)/reset-password/+page.svelte @@ -12,7 +12,7 @@ }; const formError = new FormError(); - const resetRequests = new PasswordResetService(); + const passwordResetService = PasswordResetService.resolve(); let loading = false; let showSuccessAlert = false; @@ -23,7 +23,7 @@ showSuccessAlert = false; showErrorAlert = false; loading = true; - const response = await resetRequests.create_request_async(formData.email.value); + const response = await passwordResetService.create_request_async(formData.email.value); loading = false; if (response.isCreated) { showSuccessAlert = true; @@ -37,17 +37,12 @@ } } } else { - formError.title = $LL.unexpectedError(); - formError.subtitle = $LL.tryAgainSoon(); + formError.set($LL.unexpectedError(), $LL.tryAgainSoon()); } showErrorAlert = formError.has_error() && !showSuccessAlert; } - - Reset password - Greatoffice - -

diff --git a/code/app/src/routes/(main)/(public)/reset-password/[id]/+page.svelte b/code/app/src/routes/(main)/(public)/reset-password/[id]/+page.svelte index 8f817bf..27a1af5 100644 --- a/code/app/src/routes/(main)/(public)/reset-password/[id]/+page.svelte +++ b/code/app/src/routes/(main)/(public)/reset-password/[id]/+page.svelte @@ -1,14 +1,15 @@
@@ -106,10 +106,10 @@
- {#if showErrorAlert} - + {#if form.showError} + {/if} -
+ form.submit_async()}>
@@ -136,7 +138,7 @@ id="remember-me" _pwKey={signInPageTestKeys.rememberMeCheckbox} name="remember-me" - bind:checked={formData.persist.value} + bind:checked={form.fields.persist.value} label={$LL.signInPage.notMyComputer()} />
@@ -146,7 +148,7 @@
-
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"; - -export const load: LayoutServerLoad = async ({url, request, route, cookies, locals, fetch}) => { - console.log(url.toString()); +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 ({ 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("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(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(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 -- cgit v1.3