refactor: de-duplicate code into handleResponse helper and add support for success notifications

This commit is contained in:
Derrick Hammer 2024-03-20 13:10:57 -04:00
parent 6bffca0524
commit 081df029e0
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 46 additions and 24 deletions

View File

@ -4,12 +4,15 @@ import type {
AuthActionResponse, AuthActionResponse,
CheckResponse, CheckResponse,
IdentityResponse, IdentityResponse,
OnErrorResponse OnErrorResponse,
SuccessNotificationResponse
// @ts-ignore // @ts-ignore
} from "@refinedev/core/dist/interfaces/bindings/auth" } from "@refinedev/core/dist/interfaces/bindings/auth"
import {Sdk} from "@lumeweb/portal-sdk"; import {Sdk} from "@lumeweb/portal-sdk";
import type {AccountInfoResponse} from "@lumeweb/portal-sdk"; import type {AccountInfoResponse} from "@lumeweb/portal-sdk";
;
export type AuthFormRequest = { export type AuthFormRequest = {
email: string; email: string;
password: string; password: string;
@ -43,6 +46,39 @@ export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => {
} }
}; };
type ResponseResult = {
ret: boolean | Error;
successNotification?: SuccessNotificationResponse;
redirectToSuccess?: string;
redirectToError?: string;
successCb?: () => void;
}
const handleResponse = (result: ResponseResult): AuthActionResponse => {
if (result.ret) {
if (result.ret instanceof Error) {
return {
success: false,
error: result.ret,
redirectTo: result.redirectToError
}
}
result.successCb?.();
return {
success: true,
successNotification: result.successNotification,
redirectTo: result.redirectToSuccess,
}
}
return {
success: false,
redirectTo: result.redirectToError
}
}
return { return {
async login(params: AuthFormRequest): Promise<AuthActionResponse> { async login(params: AuthFormRequest): Promise<AuthActionResponse> {
const ret = await sdk.account().login({ const ret = await sdk.account().login({
@ -68,17 +104,13 @@ export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => {
async logout(params: any): Promise<AuthActionResponse> { async logout(params: any): Promise<AuthActionResponse> {
let ret = await sdk.account().logout(); let ret = await sdk.account().logout();
return {success: ret, redirectTo: "/login"}; return handleResponse({ret, redirectToSuccess: "/login"});
}, },
async check(params?: any): Promise<CheckResponse> { async check(params?: any): Promise<CheckResponse> {
const ret = await sdk.account().ping(); const ret = await sdk.account().ping();
if (ret) { return handleResponse({ret, redirectToError: "/login", successCb: maybeSetupAuth});
maybeSetupAuth();
}
return {authenticated: ret, redirectTo: ret ? undefined : "/login"};
}, },
async onError(error: any): Promise<OnErrorResponse> { async onError(error: any): Promise<OnErrorResponse> {
@ -92,7 +124,7 @@ export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => {
first_name: params.firstName, first_name: params.firstName,
last_name: params.lastName, last_name: params.lastName,
}); });
return {success: ret, redirectTo: ret ? "/dashboard" : undefined}; return handleResponse({ret, redirectToSuccess: "/login"});
}, },
async forgotPassword(params: any): Promise<AuthActionResponse> { async forgotPassword(params: any): Promise<AuthActionResponse> {
@ -103,22 +135,12 @@ export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => {
maybeSetupAuth(); maybeSetupAuth();
const ret = await sdk.account().updatePassword(params.currentPassword, params.password as string); const ret = await sdk.account().updatePassword(params.currentPassword, params.password as string);
if (ret) { return handleResponse({
if (ret instanceof Error) { ret, successNotification: {
return { message: "Password Updated",
success: false, description: "Your password has been updated successfully.",
error: ret
}
}
return {
success: true
}
} else {
return {
success: false
}
} }
});
}, },
async getPermissions(params?: Record<string, any>): Promise<AuthActionResponse> { async getPermissions(params?: Record<string, any>): Promise<AuthActionResponse> {