@@ -3,6 +3,7 @@ import { useSidebar } from "@/components/ui/sidebar";
33import { zodResolver } from "@hookform/resolvers/zod" ;
44import { useForm } from "react-hook-form" ;
55import { z } from "zod" ;
6+ import { useEffect } from "react" ;
67
78import { Button } from "@/components/ui/button" ;
89import {
@@ -26,20 +27,48 @@ const FormSchema = z.object({
2627} ) ;
2728
2829const Login = ( ) => {
29- let { SignInWithGoogle, loginUser } = useAuth ( ) ;
30+ let { SignInWithGoogle, loginUser, isAuthenticated , loading } = useAuth ( ) ; // Get isAuthenticated and loading
3031 const { open, isMobile } = useSidebar ( ) ;
3132 const navigate = useNavigate ( ) ;
33+
34+ useEffect ( ( ) => {
35+ if ( isAuthenticated && ! loading ) {
36+ navigate ( "/" ) ;
37+ }
38+ } , [ isAuthenticated , loading , navigate ] ) ;
39+
3240 const form = useForm ( {
3341 resolver : zodResolver ( FormSchema ) ,
3442 defaultValues : {
3543 username : "" ,
3644 password : "" ,
3745 } ,
3846 } ) ;
47+
3948 async function handleGoogleAuth ( e ) {
4049 e . preventDefault ( ) ;
4150 await SignInWithGoogle ( ) ;
4251 navigate ( "/" ) ;
52+ }
53+ if ( loading ) {
54+ return (
55+ < div
56+ className = "text-foreground flex h-full flex-col"
57+ style = { {
58+ width :
59+ open && ! isMobile
60+ ? "calc(100vw - var(--sidebar-width))"
61+ : "100vw" ,
62+ } }
63+ >
64+ < div className = "m-auto" > Loading...</ div >
65+ </ div >
66+ ) ;
67+ }
68+
69+ // Don't render login form if already authenticated (will redirect)
70+ if ( isAuthenticated ) {
71+ return null ; // Or a loading spinner since useEffect will redirect
4372 }
4473
4574 return (
@@ -101,4 +130,4 @@ const Login = () => {
101130 </ div >
102131 ) ;
103132} ;
104- export default Login ;
133+ export default Login ;
0 commit comments