From a275090e03e512be9de7f1a73f1a1fb83506e4eb Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Wed, 22 Mar 2023 21:13:16 -0800 Subject: [PATCH] use singleton context to manage user state --- frontend/composables/use-auth-context.ts | 45 ++++++++++++++---------- frontend/pages/profile.vue | 5 +-- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/frontend/composables/use-auth-context.ts b/frontend/composables/use-auth-context.ts index 45048dd..aa6d96c 100644 --- a/frontend/composables/use-auth-context.ts +++ b/frontend/composables/use-auth-context.ts @@ -4,7 +4,6 @@ import { UserOut } from "~~/lib/api/types/data-contracts"; import { UserClient } from "~~/lib/api/user"; export interface IAuthContext { - self?: UserOut; get token(): string | null; get expiresAt(): string | null; get attachmentToken(): string | null; @@ -41,6 +40,9 @@ export interface IAuthContext { } class AuthContext implements IAuthContext { + // eslint-disable-next-line no-use-before-define + private static _instance?: AuthContext; + user?: UserOut; private _token: CookieRef; private _expiresAt: CookieRef; @@ -58,14 +60,18 @@ class AuthContext implements IAuthContext { return this._attachmentToken.value; } - constructor( - token: CookieRef, - expiresAt: CookieRef, - attachmentToken: CookieRef - ) { - this._token = token; - this._expiresAt = expiresAt; - this._attachmentToken = attachmentToken; + private constructor(token: string, expiresAt: string, attachmentToken: string) { + this._token = useCookie(token); + this._expiresAt = useCookie(expiresAt); + this._attachmentToken = useCookie(attachmentToken); + } + + static get instance() { + if (!this._instance) { + this._instance = new AuthContext("hb.auth.token", "hb.auth.expires_at", "hb.auth.attachment_token"); + } + + return this._instance; } isExpired() { @@ -81,14 +87,21 @@ class AuthContext implements IAuthContext { } isAuthorized() { - return this._token.value !== null && !this.isExpired(); + return !!this._token.value && !this.isExpired(); } invalidateSession() { this.user = undefined; - this._token.value = null; - this._expiresAt.value = null; - this._attachmentToken.value = null; + + // Delete the cookies + // @ts-expect-error + this._token.value = undefined; + // @ts-expect-error + this._expiresAt.value = undefined; + // @ts-expect-error + this._attachmentToken.value = undefined; + + console.log("Session invalidated"); } async login(api: PublicApi, email: string, password: string) { @@ -121,9 +134,5 @@ class AuthContext implements IAuthContext { } export function useAuthContext(): IAuthContext { - const tokenCookie = useCookie("hb.auth.token"); - const expiresAtCookie = useCookie("hb.auth.expires_at"); - const attachmentTokenCookie = useCookie("hb.auth.attachment_token"); - - return new AuthContext(tokenCookie, expiresAtCookie, attachmentTokenCookie); + return AuthContext.instance; } diff --git a/frontend/pages/profile.vue b/frontend/pages/profile.vue index 934115e..d597e92 100644 --- a/frontend/pages/profile.vue +++ b/frontend/pages/profile.vue @@ -82,14 +82,15 @@ const auth = useAuthContext(); const details = computed(() => { + console.log(auth.user); return [ { name: "Name", - text: auth.self?.name || "Unknown", + text: auth.user?.name || "Unknown", }, { name: "Email", - text: auth.self?.email || "Unknown", + text: auth.user?.email || "Unknown", }, ] as Detail[]; });