2024-03-13 17:16:52 +00:00
|
|
|
import type {AuthProvider} 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"
|
2024-03-13 17:16:52 +00:00
|
|
|
import {Sdk} from "@lumeweb/portal-sdk";
|
2024-03-13 23:08:14 +00:00
|
|
|
import Cookies from 'universal-cookie';
|
2024-03-14 11:10:17 +00:00
|
|
|
import type {AccountInfoResponse} from "@lumeweb/portal-sdk";
|
2024-03-13 17:16:52 +00:00
|
|
|
|
|
|
|
export type AuthFormRequest = {
|
|
|
|
email: string;
|
|
|
|
password: string;
|
|
|
|
rememberMe: boolean;
|
2024-03-13 23:23:29 +00:00
|
|
|
redirectTo?: string;
|
2024-03-13 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-03-13 17:16:52 +00:00
|
|
|
export class PortalAuthProvider implements RequiredAuthProvider {
|
|
|
|
private sdk: Sdk;
|
|
|
|
|
|
|
|
constructor(apiUrl: string) {
|
|
|
|
this.sdk = Sdk.create(apiUrl);
|
|
|
|
|
|
|
|
const methods: Array<keyof AuthProvider> = [
|
|
|
|
'login',
|
|
|
|
'logout',
|
|
|
|
'check',
|
|
|
|
'onError',
|
|
|
|
'register',
|
|
|
|
'forgotPassword',
|
|
|
|
'updatePassword',
|
|
|
|
'getPermissions',
|
|
|
|
'getIdentity',
|
|
|
|
];
|
|
|
|
|
|
|
|
methods.forEach((method) => {
|
2024-03-13 23:08:14 +00:00
|
|
|
this[method] = this[method]?.bind(this) as any;
|
2024-03-13 17:16:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async login(params: AuthFormRequest): Promise<AuthActionResponse> {
|
|
|
|
const ret = await this.sdk.account().login({
|
|
|
|
email: params.email,
|
|
|
|
password: params.password,
|
|
|
|
})
|
2024-03-13 23:08:14 +00:00
|
|
|
|
2024-03-14 11:10:17 +00:00
|
|
|
let redirectTo: string | undefined;
|
2024-03-13 23:23:29 +00:00
|
|
|
|
2024-03-13 23:08:14 +00:00
|
|
|
if (ret) {
|
2024-03-13 23:23:29 +00:00
|
|
|
redirectTo = params.redirectTo;
|
|
|
|
if (!redirectTo) {
|
|
|
|
redirectTo = ret ? "/dashboard" : "/login";
|
|
|
|
}
|
2024-03-13 23:08:14 +00:00
|
|
|
}
|
2024-03-13 23:23:29 +00:00
|
|
|
|
2024-03-13 17:16:52 +00:00
|
|
|
return {
|
|
|
|
success: ret,
|
2024-03-13 23:23:29 +00:00
|
|
|
redirectTo,
|
2024-03-13 17:16:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async logout(params: any): Promise<AuthActionResponse> {
|
|
|
|
let ret = await this.sdk.account().logout();
|
2024-03-13 23:08:14 +00:00
|
|
|
if (ret) {
|
|
|
|
const cookies = new Cookies();
|
|
|
|
cookies.remove('jwt');
|
|
|
|
}
|
2024-03-13 17:16:52 +00:00
|
|
|
return {success: ret, redirectTo: "/login"};
|
|
|
|
}
|
|
|
|
|
|
|
|
async check(params?: any): Promise<CheckResponse> {
|
2024-03-16 19:41:31 +00:00
|
|
|
this.maybeSetupAuth();
|
2024-03-13 23:08:14 +00:00
|
|
|
|
2024-03-13 17:16:52 +00:00
|
|
|
const ret = await this.sdk.account().ping();
|
|
|
|
|
2024-03-13 23:08:14 +00:00
|
|
|
if (!ret) {
|
2024-03-16 19:41:31 +00:00
|
|
|
const cookies = new Cookies();
|
2024-03-13 23:08:14 +00:00
|
|
|
cookies.remove('jwt');
|
|
|
|
}
|
|
|
|
|
2024-03-14 11:10:17 +00:00
|
|
|
return {authenticated: ret, redirectTo: ret ? undefined : "/login"};
|
2024-03-13 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onError(error: any): Promise<OnErrorResponse> {
|
2024-03-16 19:41:31 +00:00
|
|
|
const cookies = new Cookies();
|
|
|
|
cookies.remove('jwt');
|
|
|
|
this.sdk.setAuthToken('');
|
2024-03-13 17:16:52 +00:00
|
|
|
return {logout: true};
|
|
|
|
}
|
|
|
|
|
2024-03-13 23:06:50 +00:00
|
|
|
async register(params: RegisterFormRequest): Promise<AuthActionResponse> {
|
|
|
|
const ret = await this.sdk.account().register({
|
|
|
|
email: params.email,
|
|
|
|
password: params.password,
|
|
|
|
first_name: params.firstName,
|
|
|
|
last_name: params.lastName,
|
|
|
|
});
|
|
|
|
return {success: ret, redirectTo: ret ? "/dashboard" : undefined};
|
2024-03-13 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async forgotPassword(params: any): Promise<AuthActionResponse> {
|
|
|
|
return {success: true};
|
|
|
|
}
|
|
|
|
|
|
|
|
async updatePassword(params: any): Promise<AuthActionResponse> {
|
|
|
|
return {success: true};
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPermissions(params?: Record<string, any>): Promise<AuthActionResponse> {
|
|
|
|
return {success: true};
|
|
|
|
}
|
|
|
|
|
2024-03-14 11:10:17 +00:00
|
|
|
async getIdentity(params?: Identity): Promise<IdentityResponse> {
|
2024-03-16 19:41:31 +00:00
|
|
|
this.maybeSetupAuth();
|
2024-03-14 11:10:17 +00:00
|
|
|
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,
|
|
|
|
};
|
2024-03-13 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 19:41:31 +00:00
|
|
|
maybeSetupAuth(): void {
|
|
|
|
const cookies = new Cookies();
|
2024-03-17 12:18:33 +00:00
|
|
|
const jwtCookie = cookies.get('auth_token');
|
2024-03-16 19:41:31 +00:00
|
|
|
if (jwtCookie) {
|
|
|
|
this.sdk.setAuthToken(jwtCookie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 17:16:52 +00:00
|
|
|
public static create(apiUrl: string): AuthProvider {
|
|
|
|
return new PortalAuthProvider(apiUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RequiredAuthProvider extends AuthProvider {
|
|
|
|
login: AuthProvider['login'];
|
|
|
|
logout: AuthProvider['logout'];
|
|
|
|
check: AuthProvider['check'];
|
|
|
|
onError: AuthProvider['onError'];
|
|
|
|
register: AuthProvider['register'];
|
|
|
|
forgotPassword: AuthProvider['forgotPassword'];
|
|
|
|
updatePassword: AuthProvider['updatePassword'];
|
|
|
|
getPermissions: AuthProvider['getPermissions'];
|
|
|
|
getIdentity: AuthProvider['getIdentity'];
|
2024-03-07 14:04:59 +00:00
|
|
|
}
|