refactor: switch to reading cookie only and let server handle it for security, add maybeSetupAuth helper.

This commit is contained in:
Derrick Hammer 2024-03-16 15:41:31 -04:00
parent 6506917ddb
commit 20533913bd
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 14 additions and 9 deletions

View File

@ -56,7 +56,6 @@ export class PortalAuthProvider implements RequiredAuthProvider {
}
async login(params: AuthFormRequest): Promise<AuthActionResponse> {
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<CheckResponse> {
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<OnErrorResponse> {
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<IdentityResponse> {
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);
}