-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.js
77 lines (70 loc) · 2.15 KB
/
firebase.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Using a popup.
const database = firebase.firestore()
const auth = firebase.auth()
const provider = new firebase.auth.GoogleAuthProvider()
provider.addScope('profile')
provider.addScope('email')
window.onload = function() {
initializeApp()
}
function initializeApp() {
auth.onAuthStateChanged(function(user) {
if (user) {
const avatarSrc = user.photoURL
const name = user.displayName
const email = user.email
const userId = user.uid
const userInfo = {
name: name,
id: userId,
photoURL: avatarSrc,
email: email
}
addUserToDatabase(userInfo, userId)
updateSignIn(avatarSrc)
// getUserInfo(userId)
}
else {
updateSignOut()
}
})
}
function signInWithGoogle(type) {
console.log('sign in')
auth.signInWithPopup(provider)
.then(function(result) {
console.log('result')
// // This gives you a Google Access Token.
// var token = result.credential.accessToken
// // The signed-in user info.
// var user = result.user
// const avatarSrc = user.photoURL
// const name = user.displayName
// const email = user.email
// const userId = user.uid
// const userInfo = {
// name: name,
// id: userId,
// photoURL: avatarSrc,
// email: email
// }
// addUserToDatabase(userInfo, userId, type)
// updateSignIn(avatarSrc)
})
.catch(err => console.log(err))
}
function addUserToDatabase(userInfo, userId, type) {
if (type) {
userInfo[type] = true
}
//get a refrence to the collection you need
const userCollectionRef = database.collection('users')
//create a document in that collection
const newUserRef = userCollectionRef.doc(userId)
//set the info equal to what you want
newUserRef.set(userInfo, {merge: true})
}
function signOutWithGoogle() {
auth.signOut()
console.log('sign out')
}