From 740d2760714ede4aa3c59ed9150ecf39762b1b90 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 26 Mar 2024 11:36:24 -0400 Subject: [PATCH] feat: if dev mode is enabled store jwt to local storage and fetch it as needed --- app/data/auth-provider.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/data/auth-provider.ts b/app/data/auth-provider.ts index b3a56fe..fd2ea5e 100644 --- a/app/data/auth-provider.ts +++ b/app/data/auth-provider.ts @@ -39,9 +39,19 @@ export interface UpdatePasswordFormRequest extends UpdatePasswordFormTypes { export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => { const maybeSetupAuth = (): void => { - const jwt = sdk.account().jwtToken; + let jwt = sdk.account().jwtToken; if (jwt) { sdk.setAuthToken(jwt); + if (import.meta.env.DEV) { + localStorage.setItem("jwt", jwt); + } + } + + if (import.meta.env.DEV) { + let jwt = localStorage.getItem("jwt"); + if (jwt) { + sdk.setAuthToken(jwt); + } } }; @@ -100,6 +110,8 @@ export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => { password: params.password, }); + maybeSetupAuth(); + return handleResponse({ ret, redirectToSuccess: "/dashboard", redirectToError: "/login", successCb: () => { sdk.setAuthToken(sdk.account().jwtToken); @@ -113,11 +125,20 @@ export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => { async logout(params: any): Promise { let ret = await sdk.account().logout(); + + if (ret){ + if (import.meta.env.DEV) { + localStorage.removeItem("jwt"); + } + } + return handleResponse({ret, redirectToSuccess: "/login"}); }, async check(params?: any): Promise { + maybeSetupAuth(); const ret = await sdk.account().ping(); + maybeSetupAuth(); return handleCheckResponse({ret, redirectToError: "/login", successCb: maybeSetupAuth}); },