refactor: add sdk getter

This commit is contained in:
Derrick Hammer 2024-03-18 10:08:57 -04:00
parent d3e847baf8
commit 7c332d0f43
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 17 additions and 13 deletions

View File

@ -33,10 +33,8 @@ export type Identity = {
} }
export class PortalAuthProvider implements RequiredAuthProvider { export class PortalAuthProvider implements RequiredAuthProvider {
private sdk: Sdk;
constructor(apiUrl: string) { constructor(apiUrl: string) {
this.sdk = Sdk.create(apiUrl); this._sdk = Sdk.create(apiUrl);
const methods: Array<keyof AuthProvider> = [ const methods: Array<keyof AuthProvider> = [
'login', 'login',
@ -55,8 +53,18 @@ export class PortalAuthProvider implements RequiredAuthProvider {
}); });
} }
private _sdk: Sdk;
get sdk(): Sdk {
return this._sdk;
}
public static create(apiUrl: string): AuthProvider {
return new PortalAuthProvider(apiUrl);
}
async login(params: AuthFormRequest): Promise<AuthActionResponse> { async login(params: AuthFormRequest): Promise<AuthActionResponse> {
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,
}) })
@ -77,14 +85,14 @@ export class PortalAuthProvider implements RequiredAuthProvider {
} }
async logout(params: any): Promise<AuthActionResponse> { async logout(params: any): Promise<AuthActionResponse> {
let ret = await this.sdk.account().logout(); let ret = await this._sdk.account().logout();
return {success: ret, redirectTo: "/login"}; return {success: ret, redirectTo: "/login"};
} }
async check(params?: any): Promise<CheckResponse> { async check(params?: any): Promise<CheckResponse> {
this.maybeSetupAuth(); this.maybeSetupAuth();
const ret = await this.sdk.account().ping(); const ret = await this._sdk.account().ping();
return {authenticated: ret, redirectTo: ret ? undefined : "/login"}; return {authenticated: ret, redirectTo: ret ? undefined : "/login"};
} }
@ -95,7 +103,7 @@ export class PortalAuthProvider implements RequiredAuthProvider {
} }
async register(params: RegisterFormRequest): Promise<AuthActionResponse> { async register(params: RegisterFormRequest): Promise<AuthActionResponse> {
const ret = await this.sdk.account().register({ const ret = await this._sdk.account().register({
email: params.email, email: params.email,
password: params.password, password: params.password,
first_name: params.firstName, first_name: params.firstName,
@ -118,7 +126,7 @@ export class PortalAuthProvider implements RequiredAuthProvider {
async getIdentity(params?: Identity): Promise<IdentityResponse> { async getIdentity(params?: Identity): Promise<IdentityResponse> {
this.maybeSetupAuth(); this.maybeSetupAuth();
const ret = await this.sdk.account().info(); const ret = await this._sdk.account().info();
if (!ret) { if (!ret) {
return {identity: null}; return {identity: null};
@ -138,13 +146,9 @@ export class PortalAuthProvider implements RequiredAuthProvider {
const cookies = new Cookies(); const cookies = new Cookies();
const jwtCookie = cookies.get('auth_token'); const jwtCookie = cookies.get('auth_token');
if (jwtCookie) { if (jwtCookie) {
this.sdk.setAuthToken(jwtCookie); this._sdk.setAuthToken(jwtCookie);
} }
} }
public static create(apiUrl: string): AuthProvider {
return new PortalAuthProvider(apiUrl);
}
} }
interface RequiredAuthProvider extends AuthProvider { interface RequiredAuthProvider extends AuthProvider {