refactor: handle redirects from ?to after login, or when already logged in

This commit is contained in:
Derrick Hammer 2024-03-13 19:23:29 -04:00
parent 4c972d289d
commit 48bb9d1121
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 24 additions and 6 deletions

View File

@ -9,6 +9,7 @@ export type AuthFormRequest = {
email: string; email: string;
password: string; password: string;
rememberMe: boolean; rememberMe: boolean;
redirectTo?: string;
} }
export type RegisterFormRequest = { export type RegisterFormRequest = {
@ -48,12 +49,19 @@ export class PortalAuthProvider implements RequiredAuthProvider {
password: params.password, password: params.password,
}) })
let redirectTo:string | undefined;
if (ret) { if (ret) {
cookies.set('jwt', this.sdk.account().jwtToken, { path: '/' }); cookies.set('jwt', this.sdk.account().jwtToken, { path: '/' });
redirectTo = params.redirectTo;
if (!redirectTo) {
redirectTo = ret ? "/dashboard" : "/login";
} }
}
return { return {
success: ret, success: ret,
redirectTo: ret ? "/dashboard" : undefined, redirectTo,
}; };
} }

View File

@ -9,7 +9,7 @@ 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 {useGo, useIsAuthenticated, useLogin, useParsed} from "@refinedev/core";
import {AuthFormRequest} from "~/data/auth-provider.js"; import {AuthFormRequest} from "~/data/auth-provider.js";
import {useEffect} from "react"; import {useEffect} from "react";
@ -20,17 +20,26 @@ export const meta: MetaFunction = () => {
] ]
} }
type LoginParams = {
to: string;
}
export default function Login() { export default function Login() {
const location = useLocation() const location = useLocation()
const {isLoading: isAuthLoading, data: authData} = useIsAuthenticated(); const {isLoading: isAuthLoading, data: authData} = useIsAuthenticated();
const auth = useIsAuthenticated();
const hash = location.hash const hash = location.hash
const go = useGo(); const go = useGo();
const parsed = useParsed<LoginParams>()
useEffect(() => { useEffect(() => {
if (!isAuthLoading) { if (!isAuthLoading) {
if (authData?.authenticated) { if (authData?.authenticated) {
go({to: "/dashboard", type: "replace"}); let to = "/dashboard";
if(parsed.params?.to){
to = parsed.params.to
}
go({to, type: "push"});
} }
} }
}, [isAuthLoading, authData]); }, [isAuthLoading, authData]);
@ -89,7 +98,7 @@ const LoginSchema = z.object({
const LoginForm = () => { const LoginForm = () => {
const login = useLogin<AuthFormRequest>() const login = useLogin<AuthFormRequest>()
const parsed = useParsed<LoginParams>()
const [form, fields] = useForm({ const [form, fields] = useForm({
id: "login", id: "login",
constraint: getZodConstraint(LoginSchema), constraint: getZodConstraint(LoginSchema),
@ -104,7 +113,8 @@ const LoginForm = () => {
login.mutate({ login.mutate({
email: data.email.toString(), email: data.email.toString(),
password: data.password.toString(), password: data.password.toString(),
rememberMe: data.rememberMe.toString() === "on" rememberMe: data.rememberMe.toString() === "on",
redirectTo: parsed.params?.to
}); });
} }
}) })