portal-dashboard/app/data/auth-provider.ts

147 lines
4.0 KiB
TypeScript
Raw Normal View History

import type {AuthProvider, UpdatePasswordFormTypes} from "@refinedev/core"
2024-03-14 11:10:17 +00:00
import type {
AuthActionResponse,
CheckResponse,
IdentityResponse,
OnErrorResponse
// @ts-ignore
} from "@refinedev/core/dist/interfaces/bindings/auth"
import {Sdk} from "@lumeweb/portal-sdk";
2024-03-14 11:10:17 +00:00
import type {AccountInfoResponse} from "@lumeweb/portal-sdk";
export type AuthFormRequest = {
email: string;
password: string;
rememberMe: boolean;
redirectTo?: string;
}
2024-03-13 23:06:50 +00:00
export type RegisterFormRequest = {
email: string;
password: string;
firstName: string;
lastName: string;
}
2024-03-14 11:10:17 +00:00
export type Identity = {
id: string;
firstName: string;
lastName: string;
email: string;
}
export interface UpdatePasswordFormRequest extends UpdatePasswordFormTypes{
2024-03-19 14:12:13 +00:00
currentPassword: string;
}
export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => {
const maybeSetupAuth = (): void => {
const jwt = sdk.account().jwtToken;
if (jwt) {
sdk.setAuthToken(jwt);
2024-03-13 23:08:14 +00:00
}
};
return {
async login(params: AuthFormRequest): Promise<AuthActionResponse> {
const ret = await sdk.account().login({
email: params.email,
password: params.password,
});
let redirectTo: string | undefined;
if (ret) {
redirectTo = params.redirectTo;
if (!redirectTo) {
redirectTo = ret ? "/dashboard" : "/login";
}
sdk.setAuthToken(sdk.account().jwtToken);
}
return {
success: ret,
redirectTo,
};
},
async logout(params: any): Promise<AuthActionResponse> {
let ret = await sdk.account().logout();
2024-03-19 14:12:13 +00:00
return {success: ret, redirectTo: "/login"};
},
2024-03-14 11:10:17 +00:00
async check(params?: any): Promise<CheckResponse> {
const ret = await sdk.account().ping();
2024-03-14 11:10:17 +00:00
if (ret) {
maybeSetupAuth();
}
2024-03-19 14:12:13 +00:00
return {authenticated: ret, redirectTo: ret ? undefined : "/login"};
},
async onError(error: any): Promise<OnErrorResponse> {
2024-03-19 14:12:13 +00:00
return {logout: true};
},
async register(params: RegisterFormRequest): Promise<AuthActionResponse> {
const ret = await sdk.account().register({
email: params.email,
password: params.password,
first_name: params.firstName,
last_name: params.lastName,
});
2024-03-19 14:12:13 +00:00
return {success: ret, redirectTo: ret ? "/dashboard" : undefined};
},
async forgotPassword(params: any): Promise<AuthActionResponse> {
2024-03-19 14:12:13 +00:00
return {success: true};
},
2024-03-19 14:12:13 +00:00
async updatePassword(params: UpdatePasswordFormRequest): Promise<AuthActionResponse> {
maybeSetupAuth();
const ret = await sdk.account().updatePassword(params.currentPassword, params.password as string);
2024-03-19 14:12:13 +00:00
if (ret) {
if (ret instanceof Error) {
return {
success: false,
error: ret
}
}
return {
success: true
}
} else {
return {
success: false
}
}
},
async getPermissions(params?: Record<string, any>): Promise<AuthActionResponse> {
2024-03-19 14:12:13 +00:00
return {success: true};
},
async getIdentity(params?: Identity): Promise<IdentityResponse> {
maybeSetupAuth();
const ret = await sdk.account().info();
if (!ret) {
2024-03-19 14:12:13 +00:00
return {identity: null};
}
const acct = ret as AccountInfoResponse;
return {
id: acct.id,
firstName: acct.first_name,
lastName: acct.last_name,
email: acct.email,
};
},
};
};