-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirebase-auth.js
59 lines (49 loc) · 1.3 KB
/
firebase-auth.js
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
59
import { app } from './firebase-config.js'
import {
GoogleAuthProvider,
getAuth,
signInWithPopup,
signOut,
} from "firebase/auth";
import {
addNewUser,
userExists
} from './src/controllers/userController.js'
const auth = getAuth(app);
const googleProvider = new GoogleAuthProvider();
const registerWithGoogle = async () => {
const result = await signInWithPopup(auth, googleProvider);
// This gives you a Google Access Token.
//const token = result.credential.accessToken;
// This gives you the signed-in user.
const user = result.user;
// Check if the user is new or existing.
const isOldUser = await userExists(user.uid)
if (!isOldUser) {
// User's display name
const displayName = user.displayName;
// User's profile picture URL
const photoURL = user.photoURL;
console.log('New user signed up with Google:', user.uid);
const userData = {
name : displayName,
userId : user.uid,
email : user.email,
ppUrl : photoURL
}
addNewUser(userData)
} else {
console.log('Existing user signed in with Google:', user.uid);
}
}
const logout = () => {
signOut(auth);
};
export {
auth,
registerWithGoogle,
// logInWithEmailAndPassword,
// registerWithEmailAndPassword,
// sendPasswordReset,
logout,
};