Skip to content

Instantly share code, notes, and snippets.

@adg29
Created January 29, 2022 02:25
Show Gist options
  • Save adg29/7f090edac670ac637df34d632439d58b to your computer and use it in GitHub Desktop.
Save adg29/7f090edac670ac637df34d632439d58b to your computer and use it in GitHub Desktop.
AuthUserProvider.tsx: Typescript woes
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