Compare commits

..

4 Commits

3 changed files with 56 additions and 41 deletions

View File

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

View File

@ -1,19 +1,12 @@
import { useGo, useIsAuthenticated } from "@refinedev/core";
import {Authenticated} from "@refinedev/core";
import {Navigate} from "@remix-run/react";
export default function Index() {
const { isLoading, data } = useIsAuthenticated();
const go = useGo();
if (isLoading) {
return <>Checking Login Status</>;
}
if (data?.authenticated) {
go({ to: "/dashboard", type: "replace" });
} else {
go({ to: "/login", type: "replace" });
}
return <>Redirecting</>;
return (
<Authenticated v3LegacyAuthProviderCompatible key={"index"} loading={
<>Checking Login Status</>
}>
<Navigate to="/dashboard" replace/>
</Authenticated>
)
}

View File

@ -16,7 +16,7 @@
"@conform-to/react": "^1.0.2",
"@conform-to/zod": "^1.0.2",
"@fontsource-variable/manrope": "^5.0.19",
"@lumeweb/portal-sdk": "0.0.0-20240319140708",
"@lumeweb/portal-sdk": "0.0.0-20240320165911",
"@radix-ui/react-avatar": "^1.0.4",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-dialog": "^1.0.5",