feat: if dev mode is enabled store jwt to local storage and fetch it as needed

This commit is contained in:
Derrick Hammer 2024-03-26 11:36:24 -04:00
parent e6d296d6af
commit 740d276071
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 22 additions and 1 deletions

View File

@ -39,9 +39,19 @@ export interface UpdatePasswordFormRequest extends UpdatePasswordFormTypes {
export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => { export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => {
const maybeSetupAuth = (): void => { const maybeSetupAuth = (): void => {
const jwt = sdk.account().jwtToken; let jwt = sdk.account().jwtToken;
if (jwt) { if (jwt) {
sdk.setAuthToken(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, password: params.password,
}); });
maybeSetupAuth();
return handleResponse({ return handleResponse({
ret, redirectToSuccess: "/dashboard", redirectToError: "/login", successCb: () => { ret, redirectToSuccess: "/dashboard", redirectToError: "/login", successCb: () => {
sdk.setAuthToken(sdk.account().jwtToken); sdk.setAuthToken(sdk.account().jwtToken);
@ -113,11 +125,20 @@ export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => {
async logout(params: any): Promise<AuthActionResponse> { async logout(params: any): Promise<AuthActionResponse> {
let ret = await sdk.account().logout(); let ret = await sdk.account().logout();
if (ret){
if (import.meta.env.DEV) {
localStorage.removeItem("jwt");
}
}
return handleResponse({ret, redirectToSuccess: "/login"}); return handleResponse({ret, redirectToSuccess: "/login"});
}, },
async check(params?: any): Promise<CheckResponse> { async check(params?: any): Promise<CheckResponse> {
maybeSetupAuth();
const ret = await sdk.account().ping(); const ret = await sdk.account().ping();
maybeSetupAuth();
return handleCheckResponse({ret, redirectToError: "/login", successCb: maybeSetupAuth}); return handleCheckResponse({ret, redirectToError: "/login", successCb: maybeSetupAuth});
}, },