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 --- .../src/Endpoints/Internal/Root/ValidateRoute.cs | 2 +- code/app/src/components/button.svelte | 24 +-- code/app/src/configuration/index.ts | 4 +- code/app/src/help/cache.ts | 38 +++++ code/app/src/help/global-state.ts | 22 +++ code/app/src/help/md5.ts | 48 ++++++ code/app/src/help/persistent-store.ts | 9 +- code/app/src/models/internal/IForm.ts | 15 ++ 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 ++++----- .../src/services/abstractions/IAccountService.ts | 10 +- code/app/src/services/account-service.ts | 28 ++-- code/app/src/services/password-reset-service.ts | 17 +- code/app/src/services/settings-service.ts | 5 +- 20 files changed, 417 insertions(+), 273 deletions(-) create mode 100644 code/app/src/help/cache.ts create mode 100644 code/app/src/help/global-state.ts create mode 100644 code/app/src/help/md5.ts create mode 100644 code/app/src/models/internal/IForm.ts 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') diff --git a/code/api/src/Endpoints/Internal/Root/ValidateRoute.cs b/code/api/src/Endpoints/Internal/Root/ValidateRoute.cs index 682a869..428a1a2 100644 --- a/code/api/src/Endpoints/Internal/Root/ValidateRoute.cs +++ b/code/api/src/Endpoints/Internal/Root/ValidateRoute.cs @@ -8,7 +8,7 @@ public class ValidateRoute : RouteBaseSync.WithRequest import pwKey from "$actions/pwKey"; - import {SpinnerIcon} from "./icons"; + import { SpinnerIcon } from "./icons"; export let kind = "primary" as ButtonKind; export let size = "md" as ButtonSize; @@ -75,30 +75,30 @@ {#if href} {#if loading} - + {/if} {text} {:else} diff --git a/code/app/src/configuration/index.ts b/code/app/src/configuration/index.ts index 8e4334f..f4fb87b 100644 --- a/code/app/src/configuration/index.ts +++ b/code/app/src/configuration/index.ts @@ -57,4 +57,6 @@ export const StorageKeys = { entries: "entries", stopwatch: "stopwatchState", logLevel: "logLevel", -}; \ No newline at end of file +}; + +export type PortalMessage = "emailValidated"; \ No newline at end of file diff --git a/code/app/src/help/cache.ts b/code/app/src/help/cache.ts new file mode 100644 index 0000000..e253399 --- /dev/null +++ b/code/app/src/help/cache.ts @@ -0,0 +1,38 @@ +import { Temporal } from "temporal-polyfill"; +import { log_debug } from "$help/logger"; + +let cache = {}; + +export const CacheKeys = { + isAuthenticated: "isAuthenticated" +} + +export async function cached_result_async(key: string, staleAfterSeconds: number, get_result: any, forceRefresh: boolean = false) { + if (!cache[key]) { + cache[key] = { + l: 0, + c: undefined as T, + }; + } + const staleEpoch = ((cache[key]?.l ?? 0) + staleAfterSeconds); + const isStale = forceRefresh || (staleEpoch < Temporal.Now.instant().epochSeconds); + if (isStale || !cache[key]?.c) { + cache[key].c = await get_result(); + cache[key].l = Temporal.Now.instant().epochSeconds; + } + + log_debug("Ran cached_result_async", { + cacheKey: key, + isStale, + cache: cache[key], + staleEpoch, + }); + + return cache[key].c as T; +} + +export function clear_cache(key: string) { + if (!key) throw new Error("No key was specified"); + cache[key].c = undefined; + log_debug("Cleared cache with key: " + key); +} \ No newline at end of file diff --git a/code/app/src/help/global-state.ts b/code/app/src/help/global-state.ts new file mode 100644 index 0000000..a253ae9 --- /dev/null +++ b/code/app/src/help/global-state.ts @@ -0,0 +1,22 @@ +import { get } from "svelte/store"; +import { writable_persistent } from "./persistent-store"; + +const state = writable_persistent({ + initialState: {}, + name: "global-state" +}); + +export type GlobalStateKeys = "isLoggedIn" | "showEmailValidatedAlertWhenLoggedIn" | "all"; + +export function fgs(key: GlobalStateKeys): any { + const value = get(state); + if (key === "all") return value; + return value[key]; +} + +export function sgs(key: GlobalStateKeys, value: any) { + if (key === "all") throw new Error("Not allowed to set global state key: all"); + const stateValue = get(state); + stateValue[key] = JSON.stringify(value) + state.set(stateValue); +} \ No newline at end of file diff --git a/code/app/src/help/md5.ts b/code/app/src/help/md5.ts new file mode 100644 index 0000000..0265194 --- /dev/null +++ b/code/app/src/help/md5.ts @@ -0,0 +1,48 @@ +// A formatted version of a popular md5 implementation. +// Original copyright (c) Paul Johnston & Greg Holt. +// The function itself is now 42 lines long. +// https://stackoverflow.com/a/60467595 "Don't deny." + +export function md5(inputString: string): string { + const hc = "0123456789abcdef"; + function rh(n) { var j, s = ""; for (j = 0; j <= 3; j++) s += hc.charAt((n >> (j * 8 + 4)) & 0x0F) + hc.charAt((n >> (j * 8)) & 0x0F); return s; } + function ad(x, y) { var l = (x & 0xFFFF) + (y & 0xFFFF); var m = (x >> 16) + (y >> 16) + (l >> 16); return (m << 16) | (l & 0xFFFF); } + function rl(n, c) { return (n << c) | (n >>> (32 - c)); } + function cm(q, a, b, x, s, t) { return ad(rl(ad(ad(a, q), ad(x, t)), s), b); } + function ff(a, b, c, d, x, s, t) { return cm((b & c) | ((~b) & d), a, b, x, s, t); } + function gg(a, b, c, d, x, s, t) { return cm((b & d) | (c & (~d)), a, b, x, s, t); } + function hh(a, b, c, d, x, s, t) { return cm(b ^ c ^ d, a, b, x, s, t); } + function ii(a, b, c, d, x, s, t) { return cm(c ^ (b | (~d)), a, b, x, s, t); } + function sb(x) { + var i; var nblk = ((x.length + 8) >> 6) + 1; var blks = new Array(nblk * 16); for (i = 0; i < nblk * 16; i++) blks[i] = 0; + for (i = 0; i < x.length; i++) blks[i >> 2] |= x.charCodeAt(i) << ((i % 4) * 8); + blks[i >> 2] |= 0x80 << ((i % 4) * 8); blks[nblk * 16 - 2] = x.length * 8; return blks; + } + var i, x = sb(inputString), a = 1732584193, b = -271733879, c = -1732584194, d = 271733878, olda, oldb, oldc, oldd; + for (i = 0; i < x.length; i += 16) { + olda = a; oldb = b; oldc = c; oldd = d; + a = ff(a, b, c, d, x[i + 0], 7, -680876936); d = ff(d, a, b, c, x[i + 1], 12, -389564586); c = ff(c, d, a, b, x[i + 2], 17, 606105819); + b = ff(b, c, d, a, x[i + 3], 22, -1044525330); a = ff(a, b, c, d, x[i + 4], 7, -176418897); d = ff(d, a, b, c, x[i + 5], 12, 1200080426); + c = ff(c, d, a, b, x[i + 6], 17, -1473231341); b = ff(b, c, d, a, x[i + 7], 22, -45705983); a = ff(a, b, c, d, x[i + 8], 7, 1770035416); + d = ff(d, a, b, c, x[i + 9], 12, -1958414417); c = ff(c, d, a, b, x[i + 10], 17, -42063); b = ff(b, c, d, a, x[i + 11], 22, -1990404162); + a = ff(a, b, c, d, x[i + 12], 7, 1804603682); d = ff(d, a, b, c, x[i + 13], 12, -40341101); c = ff(c, d, a, b, x[i + 14], 17, -1502002290); + b = ff(b, c, d, a, x[i + 15], 22, 1236535329); a = gg(a, b, c, d, x[i + 1], 5, -165796510); d = gg(d, a, b, c, x[i + 6], 9, -1069501632); + c = gg(c, d, a, b, x[i + 11], 14, 643717713); b = gg(b, c, d, a, x[i + 0], 20, -373897302); a = gg(a, b, c, d, x[i + 5], 5, -701558691); + d = gg(d, a, b, c, x[i + 10], 9, 38016083); c = gg(c, d, a, b, x[i + 15], 14, -660478335); b = gg(b, c, d, a, x[i + 4], 20, -405537848); + a = gg(a, b, c, d, x[i + 9], 5, 568446438); d = gg(d, a, b, c, x[i + 14], 9, -1019803690); c = gg(c, d, a, b, x[i + 3], 14, -187363961); + b = gg(b, c, d, a, x[i + 8], 20, 1163531501); a = gg(a, b, c, d, x[i + 13], 5, -1444681467); d = gg(d, a, b, c, x[i + 2], 9, -51403784); + c = gg(c, d, a, b, x[i + 7], 14, 1735328473); b = gg(b, c, d, a, x[i + 12], 20, -1926607734); a = hh(a, b, c, d, x[i + 5], 4, -378558); + d = hh(d, a, b, c, x[i + 8], 11, -2022574463); c = hh(c, d, a, b, x[i + 11], 16, 1839030562); b = hh(b, c, d, a, x[i + 14], 23, -35309556); + a = hh(a, b, c, d, x[i + 1], 4, -1530992060); d = hh(d, a, b, c, x[i + 4], 11, 1272893353); c = hh(c, d, a, b, x[i + 7], 16, -155497632); + b = hh(b, c, d, a, x[i + 10], 23, -1094730640); a = hh(a, b, c, d, x[i + 13], 4, 681279174); d = hh(d, a, b, c, x[i + 0], 11, -358537222); + c = hh(c, d, a, b, x[i + 3], 16, -722521979); b = hh(b, c, d, a, x[i + 6], 23, 76029189); a = hh(a, b, c, d, x[i + 9], 4, -640364487); + d = hh(d, a, b, c, x[i + 12], 11, -421815835); c = hh(c, d, a, b, x[i + 15], 16, 530742520); b = hh(b, c, d, a, x[i + 2], 23, -995338651); + a = ii(a, b, c, d, x[i + 0], 6, -198630844); d = ii(d, a, b, c, x[i + 7], 10, 1126891415); c = ii(c, d, a, b, x[i + 14], 15, -1416354905); + b = ii(b, c, d, a, x[i + 5], 21, -57434055); a = ii(a, b, c, d, x[i + 12], 6, 1700485571); d = ii(d, a, b, c, x[i + 3], 10, -1894986606); + c = ii(c, d, a, b, x[i + 10], 15, -1051523); b = ii(b, c, d, a, x[i + 1], 21, -2054922799); a = ii(a, b, c, d, x[i + 8], 6, 1873313359); + d = ii(d, a, b, c, x[i + 15], 10, -30611744); c = ii(c, d, a, b, x[i + 6], 15, -1560198380); b = ii(b, c, d, a, x[i + 13], 21, 1309151649); + a = ii(a, b, c, d, x[i + 4], 6, -145523070); d = ii(d, a, b, c, x[i + 11], 10, -1120210379); c = ii(c, d, a, b, x[i + 2], 15, 718787259); + b = ii(b, c, d, a, x[i + 9], 21, -343485551); a = ad(a, olda); b = ad(b, oldb); c = ad(c, oldc); d = ad(d, oldd); + } + return rh(a) + rh(b) + rh(c) + rh(d); +} \ No newline at end of file diff --git a/code/app/src/help/persistent-store.ts b/code/app/src/help/persistent-store.ts index 6a54282..cb12547 100644 --- a/code/app/src/help/persistent-store.ts +++ b/code/app/src/help/persistent-store.ts @@ -1,6 +1,7 @@ import {browser} from "$app/environment"; import {writable as _writable, readable as _readable} from "svelte/store"; import type {Writable, Readable, StartStopNotifier} from "svelte/store"; +import { log_debug, log_info } from "./logger"; enum StoreType { SESSION = 0, @@ -76,11 +77,11 @@ function subscribe(store: Writable | Readable, options: WritableStore(options: WritableStore): Writable { if (!browser) { - console.warn("Persistent store is only available in the browser"); + log_info("WARN: Persistent store is only available in the browser"); return; } if (options.options === undefined) options.options = default_store_options; - console.log("Creating writable store with options: ", options); + log_debug("creating writable store with options: ", options); const store = _writable(options.initialState); hydrate(store, options); subscribe(store, options); @@ -89,11 +90,11 @@ function writable_persistent(options: WritableStore): Writable { function readable_persistent(options: ReadableStore): Readable { if (!browser) { - console.warn("Persistent store is only available in the browser"); + log_info("WARN: Persistent store is only available in the browser"); return; } if (options.options === undefined) options.options = default_store_options; - console.log("Creating readable store with options: ", options); + log_debug("Creating readable store with options: ", options); const store = _readable(options.initialState, options.callback); // hydrate(store, options); subscribe(store, options); diff --git a/code/app/src/models/internal/IForm.ts b/code/app/src/models/internal/IForm.ts new file mode 100644 index 0000000..c14b770 --- /dev/null +++ b/code/app/src/models/internal/IForm.ts @@ -0,0 +1,15 @@ +import type { FormError } from "./FormError"; + +export interface IForm { + fields: Record; + error: FormError; + get_payload: Function; + submit_async: Function; + isLoading: boolean; + showError: boolean; +} + +export interface IFormField { + value: any; + errors: Array; +} 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 diff --git a/code/app/src/services/abstractions/IAccountService.ts b/code/app/src/services/abstractions/IAccountService.ts index 0645eb6..ad0c45b 100644 --- a/code/app/src/services/abstractions/IAccountService.ts +++ b/code/app/src/services/abstractions/IAccountService.ts @@ -1,17 +1,13 @@ -import type {KnownProblem} from "$models/internal/KnownProblem"; -import type {Writable} from "svelte/store"; +import type { KnownProblem } from "$models/internal/KnownProblem"; +import type { Writable } from "svelte/store"; export interface IAccountService { session: Writable, - login_async(payload: LoginPayload): Promise, - logout_async(): Promise, - + end_session(callback: Function): Promise, create_account_async(payload: CreateAccountPayload): Promise, - delete_current_async(): Promise, - update_current_async(payload: UpdateAccountPayload): Promise, } diff --git a/code/app/src/services/account-service.ts b/code/app/src/services/account-service.ts index 92c6126..330e588 100644 --- a/code/app/src/services/account-service.ts +++ b/code/app/src/services/account-service.ts @@ -1,12 +1,12 @@ -import {http_delete_async, http_get_async, http_post_async} from "$api/_fetch"; -import {browser} from "$app/environment"; -import {api_base, CookieNames, StorageKeys} from "$configuration"; -import {is_known_problem} from "$models/internal/KnownProblem"; -import {log_debug} from "$help/logger"; -import {StoreType, writable_persistent} from "$help/persistent-store"; -import {get} from "svelte/store"; -import type {Writable} from "svelte/store"; -import {Temporal} from "temporal-polyfill"; +import { http_delete_async, http_get_async, http_post_async } from "$api/_fetch"; +import { browser } from "$app/environment"; +import { api_base, CookieNames, StorageKeys } from "$configuration"; +import { is_known_problem } from "$models/internal/KnownProblem"; +import { log_debug } from "$help/logger"; +import { StoreType, writable_persistent } from "$help/persistent-store"; +import { get } from "svelte/store"; +import type { Writable } from "svelte/store"; +import { Temporal } from "temporal-polyfill"; import type { CreateAccountPayload, CreateAccountResponse, @@ -38,6 +38,10 @@ export class AccountService implements IAccountService { } } + static resolve(): IAccountService { + return new AccountService(); + } + async refresh_session(forceRefresh: boolean = false): Promise { if (!this.session) return; const currentValue = get(this.session); @@ -67,7 +71,7 @@ export class AccountService implements IAccountService { async login_async(payload: LoginPayload): Promise { const response = await http_post_async(api_base("_/account/login"), payload); - if (response.ok) return {isLoggedIn: true}; + if (response.ok) return { isLoggedIn: true }; if (is_known_problem(response)) return { isLoggedIn: false, knownProblem: await response.json(), @@ -90,7 +94,7 @@ export class AccountService implements IAccountService { async create_account_async(payload: CreateAccountPayload): Promise { const response = await http_post_async(api_base("_/account/create"), payload); - if (response.ok) return {isCreated: true}; + if (response.ok) return { isCreated: true }; if (is_known_problem(response)) return { isCreated: false, knownProblem: await response.json(), @@ -109,7 +113,7 @@ export class AccountService implements IAccountService { async update_current_async(payload: UpdateAccountPayload): Promise { const response = await http_post_async(api_base("_/account/update"), payload); - if (response.ok) return {isUpdated: true}; + if (response.ok) return { isUpdated: true }; if (is_known_problem(response)) return { isUpdated: false, knownProblem: await response.json(), diff --git a/code/app/src/services/password-reset-service.ts b/code/app/src/services/password-reset-service.ts index ab3a953..d7700b3 100644 --- a/code/app/src/services/password-reset-service.ts +++ b/code/app/src/services/password-reset-service.ts @@ -1,6 +1,6 @@ -import {http_get_async, http_post_async} from "$api/_fetch"; -import {api_base} from "$configuration"; -import {is_known_problem} from "$models/internal/KnownProblem"; +import { http_get_async, http_post_async } from "$api/_fetch"; +import { api_base } from "$configuration"; +import { is_known_problem } from "$models/internal/KnownProblem"; import type { CreateRequestResponse, FulfillRequestResponse, @@ -9,9 +9,12 @@ import type { } from "./abstractions/IPasswordResetService"; export class PasswordResetService implements IPasswordResetService { + static resolve(): IPasswordResetService { + return new PasswordResetService(); + } async create_request_async(email: string): Promise { - const response = await http_post_async(api_base("_/password-reset-request/create"), {email}); - if (response.ok) return {isCreated: true}; + const response = await http_post_async(api_base("_/password-reset-request/create"), { email }); + if (response.ok) return { isCreated: true }; if (is_known_problem(response)) return { isCreated: false, knownProblem: await response.json(), @@ -23,8 +26,8 @@ export class PasswordResetService implements IPasswordResetService { } async fulfill_request_async(id: string, newPassword: string): Promise { - const response = await http_post_async(api_base("_/password-reset-request/fulfill"), {id: id, newPassword}); - if (response.ok) return {isFulfilled: true}; + const response = await http_post_async(api_base("_/password-reset-request/fulfill"), { id: id, newPassword }); + if (response.ok) return { isFulfilled: true }; if (is_known_problem(response)) return { isFulfilled: false, knownProblem: await response.json(), diff --git a/code/app/src/services/settings-service.ts b/code/app/src/services/settings-service.ts index 20053a9..7f8cb2b 100644 --- a/code/app/src/services/settings-service.ts +++ b/code/app/src/services/settings-service.ts @@ -1,6 +1,9 @@ -import type {ISettingsService} from "./abstractions/ISettingsService"; +import type { ISettingsService } from "./abstractions/ISettingsService"; export class SettingsService implements ISettingsService { + static resolve(): ISettingsService { + return new SettingsService(); + } get_user_settings(): Promise { throw new Error("Method not implemented."); } -- cgit v1.3