-
-
Save ShekMak/43e4e52a8d2aa5fc4227f2b11386dec7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { createContext, useEffect, useState } from 'react' | |
import { auth } from '../firebase/authentification'; | |
import { onAuthStateChanged, User } from 'firebase/auth'; | |
const initialUser : any = null; | |
const AuthContext = createContext({ | |
user: initialUser, | |
loading: true | |
}) | |
export function AuthContextProvider({children}: any) { | |
const [user, setUser] = useState<User | null>(null); | |
const [loading, setLoading] = useState<boolean>(true); | |
useEffect(() => { | |
let unsubscribe; | |
unsubscribe = onAuthStateChanged(auth, (user) => { | |
setUser(user); | |
setLoading(false); | |
}); | |
return unsubscribe; | |
}, []); | |
return ( | |
<AuthContext.Provider value={{user, loading}}> | |
{children} | |
</AuthContext.Provider> | |
) | |
} | |
export default AuthContext; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment