firebase auth hook/context

This commit is contained in:
shams mosowi
2019-09-08 18:35:32 +10:00
parent 6df9fd27a0
commit 659c1aac73
2 changed files with 29 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
import React from "react";
interface AuthContextInterface {
authUser: firebase.User | null | undefined;
}
const AuthContext = React.createContext<AuthContextInterface>({
authUser: undefined
});
export default AuthContext;

18
src/hooks/useAuth.ts Normal file
View File

@@ -0,0 +1,18 @@
import { useEffect, useState } from "react";
import { auth } from "../firebase";
const useAuth = () => {
const [authUser, setAuthUser] = useState<firebase.User | null | undefined>(
undefined
);
useEffect(() => {
auth.onAuthStateChanged(token => {
setAuthUser(token);
});
}, []);
return authUser;
};
export default useAuth;