Skip to content

Instantly share code, notes, and snippets.

@ShekMak
Created Feb 20, 2022
Embed
What would you like to do?
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