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> { async login(params: AuthFormRequest): Promise<AuthActionResponse> {
const cookies = new Cookies();
const ret = await this.sdk.account().login({ const ret = await this.sdk.account().login({
email: params.email, email: params.email,
password: params.password, password: params.password,
@ -65,7 +64,6 @@ export class PortalAuthProvider implements RequiredAuthProvider {
let redirectTo: string | undefined; let redirectTo: string | undefined;
if (ret) { if (ret) {
cookies.set('jwt', this.sdk.account().jwtToken, {path: '/'});
redirectTo = params.redirectTo; redirectTo = params.redirectTo;
if (!redirectTo) { if (!redirectTo) {
redirectTo = ret ? "/dashboard" : "/login"; redirectTo = ret ? "/dashboard" : "/login";
@ -88,17 +86,12 @@ export class PortalAuthProvider implements RequiredAuthProvider {
} }
async check(params?: any): Promise<CheckResponse> { async check(params?: any): Promise<CheckResponse> {
const cookies = new Cookies(); this.maybeSetupAuth();
const jwtCookie = cookies.get('jwt');
if (jwtCookie) {
this.sdk.setAuthToken(jwtCookie);
}
const ret = await this.sdk.account().ping(); const ret = await this.sdk.account().ping();
if (!ret) { if (!ret) {
const cookies = new Cookies();
cookies.remove('jwt'); cookies.remove('jwt');
} }
@ -106,6 +99,9 @@ export class PortalAuthProvider implements RequiredAuthProvider {
} }
async onError(error: any): Promise<OnErrorResponse> { async onError(error: any): Promise<OnErrorResponse> {
const cookies = new Cookies();
cookies.remove('jwt');
this.sdk.setAuthToken('');
return {logout: true}; return {logout: true};
} }
@ -132,6 +128,7 @@ export class PortalAuthProvider implements RequiredAuthProvider {
} }
async getIdentity(params?: Identity): Promise<IdentityResponse> { async getIdentity(params?: Identity): Promise<IdentityResponse> {
this.maybeSetupAuth();
const ret = await this.sdk.account().info(); const ret = await this.sdk.account().info();
if (!ret) { 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 { public static create(apiUrl: string): AuthProvider {
return new PortalAuthProvider(apiUrl); return new PortalAuthProvider(apiUrl);
} }