diff --git a/app/data/auth-provider.ts b/app/data/auth-provider.ts index e29e9fe..86cec49 100644 --- a/app/data/auth-provider.ts +++ b/app/data/auth-provider.ts @@ -56,7 +56,6 @@ export class PortalAuthProvider implements RequiredAuthProvider { } async login(params: AuthFormRequest): Promise { - const cookies = new Cookies(); const ret = await this.sdk.account().login({ email: params.email, password: params.password, @@ -65,7 +64,6 @@ export class PortalAuthProvider implements RequiredAuthProvider { let redirectTo: string | undefined; if (ret) { - cookies.set('jwt', this.sdk.account().jwtToken, {path: '/'}); redirectTo = params.redirectTo; if (!redirectTo) { redirectTo = ret ? "/dashboard" : "/login"; @@ -88,17 +86,12 @@ export class PortalAuthProvider implements RequiredAuthProvider { } async check(params?: any): Promise { - const cookies = new Cookies(); - - const jwtCookie = cookies.get('jwt'); - - if (jwtCookie) { - this.sdk.setAuthToken(jwtCookie); - } + this.maybeSetupAuth(); const ret = await this.sdk.account().ping(); if (!ret) { + const cookies = new Cookies(); cookies.remove('jwt'); } @@ -106,6 +99,9 @@ export class PortalAuthProvider implements RequiredAuthProvider { } async onError(error: any): Promise { + const cookies = new Cookies(); + cookies.remove('jwt'); + this.sdk.setAuthToken(''); return {logout: true}; } @@ -132,6 +128,7 @@ export class PortalAuthProvider implements RequiredAuthProvider { } async getIdentity(params?: Identity): Promise { + this.maybeSetupAuth(); const ret = await this.sdk.account().info(); if (!ret) { @@ -148,6 +145,14 @@ export class PortalAuthProvider implements RequiredAuthProvider { }; } + maybeSetupAuth(): void { + const cookies = new Cookies(); + const jwtCookie = cookies.get('jwt'); + if (jwtCookie) { + this.sdk.setAuthToken(jwtCookie); + } + } + public static create(apiUrl: string): AuthProvider { return new PortalAuthProvider(apiUrl); }