@@ -2,6 +2,7 @@ import React, {useCallback, useEffect, useState} from 'react';
22import './Auth.css' ;
33import { useAuth } from '../../contexts/AuthContext' ;
44import { auth } from '../../config/firebase' ;
5+ import { User as FirebaseUser } from 'firebase/auth' ;
56
67const CLIENT_ID = '87955960620-dv9h8pfv4a97mno598dcc3m1nlt0h6u4.apps.googleusercontent.com' ;
78
@@ -22,17 +23,38 @@ declare global {
2223
2324const Auth : React . FC = ( ) => {
2425 const [ isGoogleLoaded , setIsGoogleLoaded ] = useState ( false ) ;
25- const { user , login, loginWithApple} = useAuth ( ) ;
26+ const { login, loginWithApple} = useAuth ( ) ;
2627 const [ isGenerating , setIsGenerating ] = useState ( false ) ;
2728 const [ isRedirecting , setIsRedirecting ] = useState ( false ) ;
29+ const [ firebaseUser , setFirebaseUser ] = useState < FirebaseUser | null > ( null ) ;
30+ const [ checkingAuth , setCheckingAuth ] = useState ( true ) ;
2831
2932 // Get redirect URL from URL params (for CLI integration)
3033 // Parse URL parameters from the hash (e.g., #auth?callback=...)
3134 const hashParts = window . location . hash . split ( '?' ) ;
3235 const urlParams = new URLSearchParams ( hashParts [ 1 ] || '' ) ;
3336 const redirectUrl = urlParams . get ( 'redirect_url' ) || urlParams . get ( 'callback' ) ;
3437
35- const generateTokenAndRedirect = async ( ) => {
38+ // Check Firebase auth state on mount
39+ useEffect ( ( ) => {
40+ const unsubscribe = auth . onAuthStateChanged ( ( user ) => {
41+ setFirebaseUser ( user ) ;
42+ setCheckingAuth ( false ) ;
43+
44+ // If user is signed in and we have a callback URL, generate token and redirect
45+ if ( user && redirectUrl ) {
46+ generateTokenAndRedirect ( user ) ;
47+ } else if ( user && ! redirectUrl ) {
48+ // Normal web flow - redirect to account
49+ window . location . hash = 'account' ;
50+ }
51+ } ) ;
52+
53+ return ( ) => unsubscribe ( ) ;
54+ // eslint-disable-next-line react-hooks/exhaustive-deps
55+ } , [ redirectUrl ] ) ;
56+
57+ const generateTokenAndRedirect = async ( user : FirebaseUser ) => {
3658 if ( ! redirectUrl || ! user ) return ;
3759
3860 setIsGenerating ( true ) ;
@@ -72,29 +94,19 @@ const Auth: React.FC = () => {
7294 }
7395 } ;
7496
75- useEffect ( ( ) => {
76- // If user is signed in and we have a redirect URL, generate token and redirect
77- if ( user && redirectUrl && ! isGenerating && ! isRedirecting ) {
78- generateTokenAndRedirect ( ) ;
79- }
80- // eslint-disable-next-line react-hooks/exhaustive-deps
81- } , [ user , redirectUrl , isGenerating , isRedirecting ] ) ;
97+ // This useEffect is no longer needed as we handle auth state in the onAuthStateChanged above
8298
8399 const handleCredentialResponse = useCallback ( async ( response : any ) => {
84100 console . log ( 'Encoded JWT ID token: ' + response . credential ) ;
85101
86102 try {
87103 await login ( response . credential ) ;
88- // If we have a redirect URL, the useEffect will handle the redirect
89- if ( ! redirectUrl ) {
90- // Redirect to account page after successful login
91- window . location . hash = 'account' ;
92- }
104+ // Auth state change will handle the redirect automatically
93105 } catch ( error ) {
94106 console . error ( 'Failed to process login:' , error ) ;
95107 alert ( error instanceof Error ? error . message : 'Sign-in failed. Please try again.' ) ;
96108 }
97- } , [ login , redirectUrl ] ) ;
109+ } , [ login ] ) ;
98110
99111 useEffect ( ( ) => {
100112 const initializeGoogle = ( ) => {
@@ -131,19 +143,15 @@ const Auth: React.FC = () => {
131143 const handleAppleAuth = async ( ) => {
132144 try {
133145 await loginWithApple ( ) ;
134- // If we have a redirect URL, the useEffect will handle the redirect
135- if ( ! redirectUrl ) {
136- // Redirect to account page after successful login
137- window . location . hash = 'account' ;
138- }
146+ // Auth state change will handle the redirect automatically
139147 } catch ( error ) {
140148 console . error ( 'Apple sign-in failed:' , error ) ;
141149 alert ( error instanceof Error ? error . message : 'Apple sign-in failed. Please try again.' ) ;
142150 }
143151 } ;
144152
145- // Show loading state if generating token or redirecting
146- if ( isGenerating || isRedirecting ) {
153+ // Show loading state if checking auth, generating token, or redirecting
154+ if ( checkingAuth || isGenerating || isRedirecting ) {
147155 return (
148156 < div className = "auth-container" >
149157 < div className = "auth-card" >
@@ -164,7 +172,7 @@ const Auth: React.FC = () => {
164172 margin : '0 auto 16px'
165173 } } > </ div >
166174 < p style = { { color : '#d1d5db' , fontSize : '14px' } } >
167- { isRedirecting ? 'Redirecting to CLI...' : 'Generating authentication token...' }
175+ { isRedirecting ? 'Redirecting to CLI...' : isGenerating ? 'Generating authentication token...' : 'Loading ...'}
168176 </ p >
169177 { isRedirecting && (
170178 < p style = { { color : '#9ca3af' , fontSize : '12px' , marginTop : '16px' , fontStyle : 'italic' } } >
0 commit comments