Skip to content

Instantly share code, notes, and snippets.

@ShekMak
Created February 20, 2022 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShekMak/43e4e52a8d2aa5fc4227f2b11386dec7 to your computer and use it in GitHub Desktop.
Save ShekMak/43e4e52a8d2aa5fc4227f2b11386dec7 to your computer and use it in GitHub Desktop.
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