@@ -14,14 +14,23 @@ import { app } from '../../firebase';
1414import { anonymousLogin } from '../store/profile-reducer' ;
1515import { setUserCookieSession } from '../services/session-service' ;
1616
17- const AuthReadyContext = createContext ( false ) ;
17+ interface AuthSession {
18+ isAuthReady : boolean ;
19+ email : string | null ;
20+ isAuthenticated : boolean ;
21+ }
22+
23+ const AuthReadyContext = createContext < AuthSession > ( {
24+ isAuthReady : false ,
25+ email : null ,
26+ isAuthenticated : false ,
27+ } ) ;
1828
1929/**
20- * Returns true once a Firebase user (anonymous or authenticated) is
21- * available. Use this instead of registering your own
22- * `onAuthStateChanged` listener.
30+ * Returns the current auth session state once Firebase has resolved.
31+ * Use this instead of registering your own `onAuthStateChanged` listener.
2332 */
24- export function useAuthReady ( ) : boolean {
33+ export function useAuthSession ( ) : AuthSession {
2534 return useContext ( AuthReadyContext ) ;
2635}
2736
@@ -45,7 +54,11 @@ export function AuthSessionProvider({
4554 children : ReactNode ;
4655} ) : ReactElement {
4756 const dispatch = useDispatch ( ) ;
48- const [ isAuthReady , setIsAuthReady ] = useState ( false ) ;
57+ const [ session , setSession ] = useState < AuthSession > ( {
58+ isAuthReady : false ,
59+ email : null ,
60+ isAuthenticated : false ,
61+ } ) ;
4962 const intervalRef = useRef < ReturnType < typeof setInterval > | null > ( null ) ;
5063
5164 useEffect ( ( ) => {
@@ -56,7 +69,11 @@ export function AuthSessionProvider({
5669 }
5770
5871 if ( user != null ) {
59- setIsAuthReady ( true ) ;
72+ setSession ( {
73+ isAuthReady : true ,
74+ email : user . email ?? null ,
75+ isAuthenticated : ! user . isAnonymous ,
76+ } ) ;
6077 setUserCookieSession ( ) . catch ( ( ) => { } ) ;
6178
6279 // Check every 5 minutes; the cookie lasts 60 minutes, so this ensures renewal well before expiry
@@ -69,7 +86,7 @@ export function AuthSessionProvider({
6986 5 * 60 * 1000 ,
7087 ) ; // 5 minutes
7188 } else {
72- setIsAuthReady ( false ) ;
89+ setSession ( { isAuthReady : false , email : null , isAuthenticated : false } ) ;
7390 dispatch ( anonymousLogin ( ) ) ;
7491 }
7592 } ) ;
@@ -81,7 +98,7 @@ export function AuthSessionProvider({
8198 } , [ dispatch ] ) ;
8299
83100 return (
84- < AuthReadyContext . Provider value = { isAuthReady } >
101+ < AuthReadyContext . Provider value = { session } >
85102 { children }
86103 </ AuthReadyContext . Provider >
87104 ) ;
0 commit comments