feat: implement identity api

This commit is contained in:
Derrick Hammer 2024-03-14 07:10:17 -04:00
parent 0b5e066aab
commit 5f97e8c7d8
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 33 additions and 7 deletions

View File

@ -1,9 +1,15 @@
import type {AuthProvider} from "@refinedev/core"
// @ts-ignore
import type {AuthActionResponse, CheckResponse, OnErrorResponse} from "@refinedev/core/dist/interfaces/bindings/auth"
import type {
AuthActionResponse,
CheckResponse,
IdentityResponse,
OnErrorResponse
// @ts-ignore
} from "@refinedev/core/dist/interfaces/bindings/auth"
import {Sdk} from "@lumeweb/portal-sdk";
import Cookies from 'universal-cookie';
import type {AccountInfoResponse} from "@lumeweb/portal-sdk";
export type AuthFormRequest = {
email: string;
@ -19,6 +25,13 @@ export type RegisterFormRequest = {
lastName: string;
}
export type Identity = {
id: string;
firstName: string;
lastName: string;
email: string;
}
export class PortalAuthProvider implements RequiredAuthProvider {
private sdk: Sdk;
@ -49,10 +62,10 @@ export class PortalAuthProvider implements RequiredAuthProvider {
password: params.password,
})
let redirectTo:string | undefined;
let redirectTo: string | undefined;
if (ret) {
cookies.set('jwt', this.sdk.account().jwtToken, { path: '/' });
cookies.set('jwt', this.sdk.account().jwtToken, {path: '/'});
redirectTo = params.redirectTo;
if (!redirectTo) {
redirectTo = ret ? "/dashboard" : "/login";
@ -89,7 +102,7 @@ export class PortalAuthProvider implements RequiredAuthProvider {
cookies.remove('jwt');
}
return {authenticated: ret, redirectTo: ret ? undefined: "/login"};
return {authenticated: ret, redirectTo: ret ? undefined : "/login"};
}
async onError(error: any): Promise<OnErrorResponse> {
@ -118,8 +131,21 @@ export class PortalAuthProvider implements RequiredAuthProvider {
return {success: true};
}
async getIdentity(params?: any): Promise<AuthActionResponse> {
return {id: "1", fullName: "John Doe", avatar: "https://via.placeholder.com/150"};
async getIdentity(params?: Identity): Promise<IdentityResponse> {
const ret = await this.sdk.account().info();
if (!ret) {
return {identity: null};
}
const acct = ret as AccountInfoResponse;
return {
id: acct.id,
firstName: acct.first_name,
lastName: acct.last_name,
email: acct.email,
};
}
public static create(apiUrl: string): AuthProvider {