refactor: switch to pulling the cookie from the account service after ping as ping now returns it in the response

This commit is contained in:
Derrick Hammer 2024-03-18 17:25:49 -04:00
parent ba374a851c
commit 7293cb0b5a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 7 additions and 8 deletions

View File

@ -8,7 +8,6 @@ import type {
// @ts-ignore // @ts-ignore
} from "@refinedev/core/dist/interfaces/bindings/auth" } from "@refinedev/core/dist/interfaces/bindings/auth"
import {Sdk} from "@lumeweb/portal-sdk"; import {Sdk} from "@lumeweb/portal-sdk";
import Cookies from 'universal-cookie';
import type {AccountInfoResponse} from "@lumeweb/portal-sdk"; import type {AccountInfoResponse} from "@lumeweb/portal-sdk";
export type AuthFormRequest = { export type AuthFormRequest = {
@ -91,15 +90,16 @@ export class PortalAuthProvider implements RequiredAuthProvider {
} }
async check(params?: any): Promise<CheckResponse> { async check(params?: any): Promise<CheckResponse> {
this.maybeSetupAuth();
const ret = await this._sdk.account().ping(); const ret = await this._sdk.account().ping();
if(ret){
this.maybeSetupAuth();
}
return {authenticated: ret, redirectTo: ret ? undefined : "/login"}; return {authenticated: ret, redirectTo: ret ? undefined : "/login"};
} }
async onError(error: any): Promise<OnErrorResponse> { async onError(error: any): Promise<OnErrorResponse> {
const cookies = new Cookies();
return {logout: true}; return {logout: true};
} }
@ -144,10 +144,9 @@ export class PortalAuthProvider implements RequiredAuthProvider {
} }
maybeSetupAuth(): void { maybeSetupAuth(): void {
const cookies = new Cookies(); const jwt = this._sdk.account().jwtToken
const jwtCookie = cookies.get('auth_token'); if (jwt) {
if (jwtCookie) { this._sdk.setAuthToken(jwt);
this._sdk.setAuthToken(jwtCookie);
} }
} }
} }