1- import { useEffect } from 'react' ;
1+ import { useEffect , useState } from 'react' ;
22import { useLocation , useNavigate } from 'react-router-dom' ;
33import { Button } from '@/components/ui/button' ;
44import { Card , CardContent , CardDescription , CardHeader , CardTitle } from '@/components/ui/card' ;
5- import { Sparkles } from 'lucide-react' ;
5+ import { Loader2 , Sparkles } from 'lucide-react' ;
66import { useAuth } from '@/lib/supabase/AuthProvider' ;
7+ import { useToast } from '@/hooks/use-toast' ;
8+ import { SUPABASE_URL } from '@/lib/supabase/client' ;
79
810export default function LoginPage ( ) {
911 const { user, signInWithGoogle } = useAuth ( ) ;
1012 const navigate = useNavigate ( ) ;
1113 const location = useLocation ( ) ;
1214 const from = ( location . state as { from ?: { pathname ?: string } } | null ) ?. from ?. pathname || '/app' ;
15+ const { toast } = useToast ( ) ;
16+ const [ signingIn , setSigningIn ] = useState ( false ) ;
1317
1418 useEffect ( ( ) => {
1519 if ( user ) navigate ( from , { replace : true } ) ;
1620 } , [ user , navigate , from ] ) ;
1721
22+ const handleSignIn = async ( ) => {
23+ setSigningIn ( true ) ;
24+ try {
25+ await signInWithGoogle ( ) ;
26+ } catch ( err ) {
27+ toast ( {
28+ title : 'Login Error' ,
29+ description : err instanceof Error ? err . message : 'Failed to start Google sign-in' ,
30+ variant : 'destructive' ,
31+ } ) ;
32+ setSigningIn ( false ) ;
33+ }
34+ } ;
35+
36+ const handleResetAuth = ( ) => {
37+ try {
38+ const ref = new URL ( SUPABASE_URL ) . hostname . split ( '.' ) [ 0 ] ;
39+ const prefix = `sb-${ ref } -` ;
40+ for ( const key of Object . keys ( localStorage ) ) {
41+ if ( key . startsWith ( prefix ) ) localStorage . removeItem ( key ) ;
42+ }
43+ } catch ( err ) {
44+ console . warn ( '[Auth] Failed to reset local auth state:' , err ) ;
45+ }
46+ window . location . reload ( ) ;
47+ } ;
48+
1849 return (
1950 < div className = "min-h-screen flex items-center justify-center bg-background px-4" >
2051 < Card className = "w-full max-w-md" >
@@ -26,9 +57,21 @@ export default function LoginPage() {
2657 < CardDescription > Google sign-in is required to use the app.</ CardDescription >
2758 </ CardHeader >
2859 < CardContent >
29- < Button className = "w-full" onClick = { ( ) => void signInWithGoogle ( ) } >
30- Continue with Google
31- </ Button >
60+ < div className = "space-y-3" >
61+ < Button className = "w-full" onClick = { ( ) => void handleSignIn ( ) } disabled = { signingIn } >
62+ { signingIn ? (
63+ < >
64+ < Loader2 className = "h-4 w-4 mr-2 animate-spin" />
65+ Redirecting...
66+ </ >
67+ ) : (
68+ 'Continue with Google'
69+ ) }
70+ </ Button >
71+ < Button className = "w-full" variant = "outline" onClick = { handleResetAuth } disabled = { signingIn } >
72+ Having trouble? Reset login
73+ </ Button >
74+ </ div >
3275 </ CardContent >
3376 </ Card >
3477 </ div >
0 commit comments