mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-22 20:51:29 +10:00
- integrating sign in with google
- set up dark mode context and other services
This commit is contained in:
52
src/contexts/UserContext.js
Normal file
52
src/contexts/UserContext.js
Normal file
@ -0,0 +1,52 @@
|
||||
import React, { createContext, useState, useEffect } from "react";
|
||||
import { useAuthState } from "react-firebase-hooks/auth";
|
||||
import firebase from "gatsby-plugin-firebase";
|
||||
import { toast } from "react-toastify";
|
||||
|
||||
const defaultState = {
|
||||
user: null,
|
||||
logout: () => {},
|
||||
loginWithGoogle: async () => {},
|
||||
};
|
||||
|
||||
const UserContext = createContext(defaultState);
|
||||
|
||||
const UserProvider = ({ children }) => {
|
||||
const [firebaseUser, loading] = useAuthState(firebase.auth());
|
||||
const [user, setUser] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
setUser(firebaseUser);
|
||||
}, [firebaseUser]);
|
||||
|
||||
const loginWithGoogle = async () => {
|
||||
const provider = new firebase.auth.GoogleAuthProvider();
|
||||
|
||||
try {
|
||||
await firebase.auth().signInWithPopup(provider);
|
||||
} catch (error) {
|
||||
toast.error(error.message);
|
||||
}
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
firebase.auth().signOut();
|
||||
};
|
||||
|
||||
return (
|
||||
<UserContext.Provider
|
||||
value={{
|
||||
user,
|
||||
loading,
|
||||
logout,
|
||||
loginWithGoogle,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</UserContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export default UserContext;
|
||||
|
||||
export { UserProvider };
|
||||
Reference in New Issue
Block a user