feat: setup auth provider and refine with login, check and logout methods

This commit is contained in:
Derrick Hammer 2024-03-13 13:16:52 -04:00
parent 7032892686
commit 51618a2fda
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
6 changed files with 18990 additions and 195 deletions

View File

@ -1,37 +1,99 @@
import { AuthProvider } from "@refinedev/core" import type {AuthProvider} from "@refinedev/core"
import type {
AuthActionResponse,
CheckResponse,
OnErrorResponse
// @ts-ignore
} from "@refinedev/core/dist/interfaces/bindings/auth"
export const authProvider: AuthProvider = { // @ts-ignore
login: async (params: any) => { import type {AuthActionResponse, CheckResponse, OnErrorResponse} from "@refinedev/core/dist/interfaces/bindings/auth"
return { success: true } satisfies AuthActionResponse import {Sdk} from "@lumeweb/portal-sdk";
},
logout: async (params: any) => { export type AuthFormRequest = {
return { success: true } satisfies AuthActionResponse email: string;
}, password: string;
check: async (params?: any) => { rememberMe: boolean;
return { authenticated: true } satisfies CheckResponse }
},
onError: async (error: any) => { export class PortalAuthProvider implements RequiredAuthProvider {
return { logout: true } satisfies OnErrorResponse private apiUrl: string;
}, private sdk: Sdk;
register: async (params: any) => {
return { success: true } satisfies AuthActionResponse constructor(apiUrl: string) {
}, this.apiUrl = apiUrl;
forgotPassword: async (params: any) => { this.sdk = Sdk.create(apiUrl);
return { success: true } satisfies AuthActionResponse
}, const methods: Array<keyof AuthProvider> = [
updatePassword: async (params: any) => { 'login',
return { success: true } satisfies AuthActionResponse 'logout',
}, 'check',
getPermissions: async (params: any) => { 'onError',
return { success: true } satisfies AuthActionResponse 'register',
}, 'forgotPassword',
getIdentity: async (params: any) => { 'updatePassword',
return { id: "1", fullName: "John Doe", avatar: "https://via.placeholder.com/150" } 'getPermissions',
'getIdentity',
];
methods.forEach((method) => {
this[method] = this[method]?.bind(this);
});
}
async login(params: AuthFormRequest): Promise<AuthActionResponse> {
const ret = await this.sdk.account().login({
email: params.email,
password: params.password,
})
return {
success: ret,
redirectTo: ret ? "/dashboard" : undefined,
};
}
async logout(params: any): Promise<AuthActionResponse> {
let ret = await this.sdk.account().logout();
return {success: ret, redirectTo: "/login"};
}
async check(params?: any): Promise<CheckResponse> {
const ret = await this.sdk.account().ping();
return {authenticated: ret};
}
async onError(error: any): Promise<OnErrorResponse> {
return {logout: true};
}
async register(params: any): Promise<AuthActionResponse> {
return {success: true};
}
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};
}
async getIdentity(params?: any): Promise<AuthActionResponse> {
return {id: "1", fullName: "John Doe", avatar: "https://via.placeholder.com/150"};
}
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'];
}

View File

@ -12,7 +12,7 @@ import type { LinksFunction } from "@remix-run/node";
// Supports weights 200-800 // Supports weights 200-800
import '@fontsource-variable/manrope'; import '@fontsource-variable/manrope';
import {Refine} from "@refinedev/core"; import {Refine} from "@refinedev/core";
import {authProvider} from "~/data/auth-provider.js"; import {PortalAuthProvider} from "~/data/auth-provider.js";
import routerProvider from "@refinedev/remix-router"; import routerProvider from "@refinedev/remix-router";
export const links: LinksFunction = () => [ export const links: LinksFunction = () => [
@ -40,7 +40,7 @@ export function Layout({children}: { children: React.ReactNode }) {
export default function App() { export default function App() {
return ( return (
<Refine <Refine
authProvider={authProvider} authProvider={PortalAuthProvider.create("https://alpha.pinner.xyz")}
routerProvider={routerProvider} routerProvider={routerProvider}
> >
<Outlet/> <Outlet/>

View File

@ -8,8 +8,12 @@ export default function Index() {
const go = useGo(); const go = useGo();
useEffect(() => { useEffect(() => {
if (!isLoading && data?.authenticated) { if (!isLoading) {
if (data?.authenticated) {
go({to: "/dashboard", type: "replace"}); go({to: "/dashboard", type: "replace"});
} else {
go({to: "/login", type: "replace"});
}
} }
}, [isLoading, data]); }, [isLoading, data]);
@ -17,9 +21,5 @@ export default function Index() {
return <>Checking Login Status</> || null; return <>Checking Login Status</> || null;
} }
if (data?.authenticated) { return (<>Redirecting</>) || null;
return <>Redirecting</> || null;
}
return <Login />;
} }

View File

@ -9,6 +9,9 @@ import lumeBgPng from "~/images/lume-bg-image.png?url"
import {Field, FieldCheckbox} from "~/components/forms" import {Field, FieldCheckbox} from "~/components/forms"
import {getFormProps, useForm} from "@conform-to/react" import {getFormProps, useForm} from "@conform-to/react"
import {getZodConstraint, parseWithZod} from "@conform-to/zod" import {getZodConstraint, parseWithZod} from "@conform-to/zod"
import {useGo, useIsAuthenticated, useLogin} from "@refinedev/core";
import {AuthFormRequest} from "~/data/auth-provider.js";
import {useEffect} from "react";
export const meta: MetaFunction = () => { export const meta: MetaFunction = () => {
return [ return [
@ -19,7 +22,18 @@ export const meta: MetaFunction = () => {
export default function Login() { export default function Login() {
const location = useLocation() const location = useLocation()
const {isLoading: isAuthLoading, data: authData} = useIsAuthenticated();
const auth = useIsAuthenticated();
const hash = location.hash const hash = location.hash
const go = useGo();
useEffect(() => {
if (!isAuthLoading) {
if (authData?.authenticated) {
go({to: "/dashboard", type: "replace"});
}
}
}, [isAuthLoading, authData]);
return ( return (
<div className="p-10 h-screen relative"> <div className="p-10 h-screen relative">
@ -74,13 +88,25 @@ const LoginSchema = z.object({
}) })
const LoginForm = () => { const LoginForm = () => {
const login = useLogin<AuthFormRequest>()
const [form, fields] = useForm({ const [form, fields] = useForm({
id: "login", id: "login",
constraint: getZodConstraint(LoginSchema), constraint: getZodConstraint(LoginSchema),
onValidate({formData}) { onValidate({formData}) {
return parseWithZod(formData, {schema: LoginSchema}) return parseWithZod(formData, {schema: LoginSchema})
}, },
shouldValidate: "onSubmit" shouldValidate: "onSubmit",
onSubmit(e) {
e.preventDefault();
const data = Object.fromEntries(new FormData(e.currentTarget).entries());
login.mutate({
email: data.email.toString(),
password: data.password.toString(),
rememberMe: data.rememberMe.toString() === "on"
});
}
}) })
return ( return (

18707
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

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