forked from DhanushNehru/CustomCodeEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthContext.js
More file actions
58 lines (48 loc) · 1.97 KB
/
Copy pathAuthContext.js
File metadata and controls
58 lines (48 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { onAuthStateChanged, signOut, fetchSignInMethodsForEmail, signInWithPopup } from "firebase/auth";
import React, { useContext, useEffect, useState } from "react";
import { GithubAuthProvider, GoogleAuthProvider, auth } from "../components/firebase";
const AuthContext = React.createContext();
export function useAuth() {
return useContext(AuthContext);
}
export function AuthProvider({ children }) {
const [currentUser, setCurrentUser] = useState(null);
const googleSignIn = async () => {
const provider = new GoogleAuthProvider();
await signInWithPopup(auth, provider);
};
const githubSignIn = async () => {
const provider = new GithubAuthProvider();
try {
// Attempt to sign in with GitHub
const result = await signInWithPopup(auth, provider);
return result.user;
} catch (error) {
// Check if the error is for an existing account
if (error.code === "auth/account-exists-with-different-credential") {
const email = error.customData.email;
const signInMethods = await fetchSignInMethodsForEmail(auth, email);
if (signInMethods.includes("google.com")) {
// If the existing account is Google, sign in with Google instead
alert("You already have an account with this email using Google. Signing you in with Google now.");
const googleProvider = new GoogleAuthProvider();
const googleResult = await signInWithPopup(auth, googleProvider);
return googleResult.user;
} else {
alert("An account already exists with this email. Please sign in using one of the other method");
}
}
throw error;
}
};
function logOut() {
signOut(auth);
}
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (user) => {
setCurrentUser(user);
});
return () => unsubscribe();
}, []);
return <AuthContext.Provider value={{ currentUser, googleSignIn, githubSignIn, logOut }}>{children}</AuthContext.Provider>;
}