Created
January 29, 2022 02:25
-
-
Save adg29/7f090edac670ac637df34d632439d58b to your computer and use it in GitHub Desktop.
AuthUserProvider.tsx: Typescript woes
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 { useReducer, useEffect } from "react"; | |
import firebase from "firebase/compat/app"; | |
import { User as FirebaseUser } from "firebase/auth"; | |
import { | |
IAuthProvider, | |
mainReducer, | |
initialState, | |
AuthUserActionsTypes, | |
formatAuthUser, | |
authUserContext, | |
} from "./AuthUserContext"; | |
export function AuthUserProvider({ children }: IAuthProvider) { | |
const [state, dispatch] = useReducer(mainReducer, initialState); | |
const authStateChanged = async (authState: FirebaseUser | null) => { | |
if (!authState) { | |
debugger; | |
dispatch({ | |
type: AuthUserActionsTypes.FIREBASE_LOGOUT, | |
payload: null, | |
}); | |
return; | |
} | |
var formattedUser = formatAuthUser(authState); | |
debugger; | |
dispatch({ | |
type: AuthUserActionsTypes.FIREBASE_LOGIN, | |
payload: { | |
loading: false, | |
authUser: formattedUser, | |
}, | |
}); | |
}; | |
useEffect(() => { | |
const unsubscribe = firebase.auth().onAuthStateChanged(authStateChanged); | |
return () => unsubscribe(); | |
}, []); | |
return ( | |
<authUserContext.Provider value={{ state, dispatch }}> | |
{children} | |
</authUserContext.Provider> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment