Skip to content

Commit

Permalink
fix login issue
Browse files Browse the repository at this point in the history
  • Loading branch information
shadrach-tayo committed Dec 5, 2024
1 parent 4cb82f9 commit 6dd78f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/app/(unauth)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { useFormState, useFormStatus } from "react-dom";
import { login, LoginUserData } from "@/app/actions";
import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { validateAuth } from "@/lib/api";
import { useSuspenseQuery } from "@tanstack/react-query";
import { LoaderCircleIcon } from "lucide-react";

const initialState: {
email?: string;
Expand All @@ -17,15 +20,34 @@ const initialState: {
export default function Login() {
const [state, formAction] = useFormState(login, initialState);
const { pending } = useFormStatus();
const { data: isLoggedIn, isLoading, refetch } = useSuspenseQuery(validateAuth);
const router = useRouter();

// const checkAuth = async () => {};

useEffect(() => {
console.log("[auth]", { isLoggedIn, isLoading });
if (!isLoading && isLoggedIn) {
// redirect to auth page
router.push("/");
}
}, [isLoggedIn, isLoading, router]);

useEffect(() => {
if (state.user) {
router.push('/');
// router.push('/');
window.location.reload();
}
}, [router, state.user])

if (isLoading) {
return (
<div className="flex items-center justify-center w-full h-full">
<LoaderCircleIcon className="w-10 h-10" />
</div>
);
}

return (
<div className="container mx-auto max-w-md">
<div className="flex flex-col items-center justify-center h-screen">
Expand Down
2 changes: 1 addition & 1 deletion src/components/custom/LayoutPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function LayoutPanel({ children }: PropsWithChildren<unknown>) {
if (!isLoading && !isLoggedIn) {
console.log("[auth]", { isLoggedIn });
// redirect to auth page
router.push('/login')
// router.push('/login')
}
}, [isLoggedIn, isLoading, router]);

Expand Down

0 comments on commit 6dd78f6

Please sign in to comment.