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