@@ -6,25 +6,28 @@ import {
66 GoogleAuthProvider ,
77 signOut ,
88} from "firebase/auth" ;
9+ import firebaseApp from "config/firebase-config" ;
910
1011export const UserContext = createContext ( ) ;
1112
1213const UserProvider = ( { children } ) => {
1314 const [ state , dispatch ] = useReducer ( authReducer , initialState ) ;
1415
1516 useEffect ( ( ) => {
16- const unsubscriber = getAuth ( ) . onAuthStateChanged ( async ( user ) => {
17- if ( user ) {
18- resetAuth ( ) ;
19- } else {
20- dispatch ( {
21- type : RESET_AUTH_STATE ,
22- payload : {
23- loading : false ,
24- } ,
25- } ) ;
17+ const unsubscriber = getAuth ( firebaseApp ) . onAuthStateChanged (
18+ async ( user ) => {
19+ if ( user ) {
20+ resetAuth ( ) ;
21+ } else {
22+ dispatch ( {
23+ type : RESET_AUTH_STATE ,
24+ payload : {
25+ loading : false ,
26+ } ,
27+ } ) ;
28+ }
2629 }
27- } ) ;
30+ ) ;
2831 return ( ) => unsubscriber ( ) ;
2932 } , [ ] ) ;
3033
@@ -34,20 +37,21 @@ const UserProvider = ({ children }) => {
3437 Object . keys ( claims ) . filter ( ( claim ) => claim . includes ( "ROLE_" ) ) ;
3538
3639 // Login
37- const login = ( ) => signInWithPopup ( getAuth ( ) , new GoogleAuthProvider ( ) ) ;
40+ const login = ( ) =>
41+ signInWithPopup ( getAuth ( firebaseApp ) , new GoogleAuthProvider ( ) ) ;
3842
3943 // Logout
40- const logout = ( ) => signOut ( getAuth ( ) ) ;
44+ const logout = ( ) => signOut ( getAuth ( firebaseApp ) ) ;
4145
4246 const refreshToken = ( ) => {
43- getAuth ( )
47+ getAuth ( firebaseApp )
4448 . currentUser . getIdToken ( true )
4549 . then ( ( idToken ) => resetAuth ( idToken ) )
4650 . catch ( ( error ) => console . error ( error ) ) ;
4751 } ;
4852
4953 const resetAuth = ( ) => {
50- getAuth ( )
54+ getAuth ( firebaseApp )
5155 . currentUser . getIdTokenResult ( )
5256 . then ( ( idTokenResult ) => {
5357 if ( typeof idTokenResult . claims != undefined ) {
0 commit comments