fix: auth redir memoization
This commit is contained in:
@@ -5,16 +5,27 @@ import { useRouter } from "next/navigation";
|
|||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
|
||||||
export function AuthRedirect() {
|
export function AuthRedirect() {
|
||||||
const { data: session, isPending } = authClient.useSession();
|
|
||||||
// const session = { user: null }; const isPending = false;
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Only redirect if we're sure the user is authenticated
|
let isCurrent = true;
|
||||||
if (!isPending && session?.user) {
|
|
||||||
router.push("/dashboard");
|
async function redirectAuthenticatedUser() {
|
||||||
|
const { data: session } = await authClient.getSession().catch(() => ({
|
||||||
|
data: null,
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (isCurrent && session?.user) {
|
||||||
|
router.push("/dashboard");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [session, isPending, router]);
|
|
||||||
|
void redirectAuthenticatedUser();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
isCurrent = false;
|
||||||
|
};
|
||||||
|
}, [router]);
|
||||||
|
|
||||||
// This component doesn't render anything
|
// This component doesn't render anything
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user