2024-03-20 20:21:56 +00:00
|
|
|
import type {AuthProvider, HttpError, UpdatePasswordFormTypes} from "@refinedev/core"
|
2024-03-13 17:16:52 +00:00
|
|
|
|
2024-03-14 11:10:17 +00:00
|
|
|
import type {
|
|
|
|
AuthActionResponse,
|
|
|
|
CheckResponse,
|
|
|
|
IdentityResponse,
|
2024-03-20 17:10:57 +00:00
|
|
|
OnErrorResponse,
|
|
|
|
SuccessNotificationResponse
|
2024-03-14 11:10:17 +00:00
|
|
|
// @ts-ignore
|
|
|
|
} from "@refinedev/core/dist/interfaces/bindings/auth"
|
2024-03-20 20:21:56 +00:00
|
|
|
import {Sdk, AccountError} from "@lumeweb/portal-sdk";
|
2024-03-14 11:10:17 +00:00
|
|
|
import type {AccountInfoResponse} from "@lumeweb/portal-sdk";
|
2024-03-13 17:16:52 +00:00
|
|
|
|
2024-03-20 17:10:57 +00:00
|
|
|
|
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-20 17:10:57 +00:00
|
|
|
export interface UpdatePasswordFormRequest extends UpdatePasswordFormTypes {
|
2024-03-19 14:12:13 +00:00
|
|
|
currentPassword: string;
|
|
|
|
}
|
|
|
|
|
2024-03-19 11:20:11 +00:00
|
|
|
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
|
|
|
}
|
2024-03-19 11:20:11 +00:00
|
|
|
};
|
|
|
|
|
2024-03-20 17:10:57 +00:00
|
|
|
type ResponseResult = {
|
|
|
|
ret: boolean | Error;
|
|
|
|
successNotification?: SuccessNotificationResponse;
|
|
|
|
redirectToSuccess?: string;
|
|
|
|
redirectToError?: string;
|
|
|
|
successCb?: () => void;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:26:24 +00:00
|
|
|
interface CheckResponseResult extends ResponseResult {
|
|
|
|
authenticated?: boolean;
|
|
|
|
}
|
|
|
|
|
2024-03-20 17:10:57 +00:00
|
|
|
const handleResponse = (result: ResponseResult): AuthActionResponse => {
|
|
|
|
if (result.ret) {
|
2024-03-20 20:21:56 +00:00
|
|
|
if (result.ret instanceof AccountError) {
|
2024-03-20 17:10:57 +00:00
|
|
|
return {
|
|
|
|
success: false,
|
2024-03-20 20:21:56 +00:00
|
|
|
error: result.ret satisfies HttpError,
|
2024-03-20 17:10:57 +00:00
|
|
|
redirectTo: result.redirectToError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.successCb?.();
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
successNotification: result.successNotification,
|
|
|
|
redirectTo: result.redirectToSuccess,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
redirectTo: result.redirectToError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:26:24 +00:00
|
|
|
const handleCheckResponse = (result: CheckResponseResult): CheckResponse => {
|
|
|
|
const response = handleResponse(result);
|
|
|
|
const success = response.success;
|
|
|
|
delete response.success;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...response,
|
|
|
|
authenticated: success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 11:20:11 +00:00
|
|
|
return {
|
|
|
|
async login(params: AuthFormRequest): Promise<AuthActionResponse> {
|
|
|
|
const ret = await sdk.account().login({
|
|
|
|
email: params.email,
|
|
|
|
password: params.password,
|
|
|
|
});
|
|
|
|
|
2024-03-20 17:43:51 +00:00
|
|
|
return handleResponse({
|
|
|
|
ret, redirectToSuccess: "/dashboard", redirectToError: "/login", successCb: () => {
|
|
|
|
sdk.setAuthToken(sdk.account().jwtToken);
|
|
|
|
}, successNotification: {
|
|
|
|
message: "Login Successful",
|
|
|
|
description: "You have successfully logged in."
|
2024-03-19 11:20:11 +00:00
|
|
|
|
|
|
|
}
|
2024-03-20 17:43:51 +00:00
|
|
|
});
|
2024-03-19 11:20:11 +00:00
|
|
|
},
|
2024-03-18 21:25:49 +00:00
|
|
|
|
2024-03-19 11:20:11 +00:00
|
|
|
async logout(params: any): Promise<AuthActionResponse> {
|
|
|
|
let ret = await sdk.account().logout();
|
2024-03-20 17:10:57 +00:00
|
|
|
return handleResponse({ret, redirectToSuccess: "/login"});
|
2024-03-19 11:20:11 +00:00
|
|
|
},
|
2024-03-14 11:10:17 +00:00
|
|
|
|
2024-03-19 11:20:11 +00:00
|
|
|
async check(params?: any): Promise<CheckResponse> {
|
|
|
|
const ret = await sdk.account().ping();
|
2024-03-14 11:10:17 +00:00
|
|
|
|
2024-03-20 19:26:24 +00:00
|
|
|
return handleCheckResponse({ret, redirectToError: "/login", successCb: maybeSetupAuth});
|
2024-03-19 11:20:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async onError(error: any): Promise<OnErrorResponse> {
|
2024-03-19 14:12:13 +00:00
|
|
|
return {logout: true};
|
2024-03-19 11:20:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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-20 17:42:21 +00:00
|
|
|
return handleResponse({
|
|
|
|
ret, redirectToSuccess: "/login", successNotification: {
|
|
|
|
message: "Registration Successful",
|
|
|
|
description: "You have successfully registered. Please check your email to verify your account.",
|
|
|
|
}
|
|
|
|
});
|
2024-03-19 11:20:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async forgotPassword(params: any): Promise<AuthActionResponse> {
|
2024-03-19 14:12:13 +00:00
|
|
|
return {success: true};
|
2024-03-19 11:20:11 +00:00
|
|
|
},
|
|
|
|
|
2024-03-19 14:12:13 +00:00
|
|
|
async updatePassword(params: UpdatePasswordFormRequest): Promise<AuthActionResponse> {
|
|
|
|
maybeSetupAuth();
|
2024-03-19 14:23:03 +00:00
|
|
|
const ret = await sdk.account().updatePassword(params.currentPassword, params.password as string);
|
2024-03-19 14:12:13 +00:00
|
|
|
|
2024-03-20 17:10:57 +00:00
|
|
|
return handleResponse({
|
|
|
|
ret, successNotification: {
|
|
|
|
message: "Password Updated",
|
|
|
|
description: "Your password has been updated successfully.",
|
2024-03-19 14:12:13 +00:00
|
|
|
}
|
2024-03-20 17:10:57 +00:00
|
|
|
});
|
2024-03-19 11:20:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async getPermissions(params?: Record<string, any>): Promise<AuthActionResponse> {
|
2024-03-19 14:12:13 +00:00
|
|
|
return {success: true};
|
2024-03-19 11:20:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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};
|
2024-03-19 11:20:11 +00:00
|
|
|
}
|
2024-03-13 17:16:52 +00:00
|
|
|
|
2024-03-19 11:20:11 +00:00
|
|
|
const acct = ret as AccountInfoResponse;
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: acct.id,
|
|
|
|
firstName: acct.first_name,
|
|
|
|
lastName: acct.last_name,
|
|
|
|
email: acct.email,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|