-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.js
181 lines (158 loc) · 5.36 KB
/
login.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { getAuth, signOut,
createUserWithEmailAndPassword, signInWithEmailAndPassword,
signInWithPopup, FacebookAuthProvider,
GoogleAuthProvider,
} from 'firebase/auth'
import {
getFirestore, collection, doc, getDoc,
setDoc,
} from 'firebase/firestore'
// initialize firebase services
const auth = getAuth();
const db = getFirestore();
// console.log(auth)
// reference to collections
const colRefUsers = collection(db, "users")
// Registration of User using Email and Password
const appRegForm = document.querySelector('#appRegForm');
const appRegFormOutp = document.querySelector('#appRegFormTextOutp');
const pwMatchCheck = (firstPw, secondPw) => {
if (firstPw === secondPw) {
return true
} else {
return false
}
};
appRegForm.addEventListener('submit', (e) => {
e.preventDefault();
const userFirstName = appRegForm.fname.value;
const userLastName = appRegForm.lname.value;
const userEmail = appRegForm.email.value;
const userPw = appRegForm.password.value;
appRegFormOutp.innerHTML = "";
if (! pwMatchCheck(appRegForm.password.value, appRegForm.confirmPw.value)) {
appRegFormOutp.innerHTML = "Error: Passwords do not match."
} else {
createUserWithEmailAndPassword(auth, userEmail, userPw)
.then((cred) => {
console.log('user created:', cred.user);
setDoc(doc(db, 'users', userEmail), {
user_email: userEmail,
firstname: userFirstName,
lastname: userLastName,
photo: "",
bonus_tries: 0,
points: 0,
score: 0,
})
.then(() => {
console.log('new user record created')
})
appRegForm.reset();
})
.catch((err) => {
console.log(err.message);
})
};
})
// Signing in User using Email and Password
const appLoginForm = document.querySelector('#appLoginForm');
const appLoginFormOutp = document.querySelector('#appLoginFormTextOutp');
appLoginForm.addEventListener('submit', (e) => {
e.preventDefault();
const userEmail = appLoginForm.loginUsername.value;
const userPw = appLoginForm.loginPw.value;
appLoginFormOutp.innerHTML = ""
signInWithEmailAndPassword(auth, userEmail, userPw)
.then((cred) => {
console.log('user signed in:', cred.user);
})
.catch((err) => {
console.log(err.message);
if (err.message === "Firebase: Error (auth/wrong-password).") {
appLoginFormOutp.innerHTML = "Error: Wrong password."
}
})
})
// Check if user exists
// const testDoc = doc(db, 'users', 'ebalbin0@mylangara.ca');
// getDoc(testDoc)
// .then((document) => {
// if (document.exists()) {
// console.log('Document exists:', document.data())
// } else {
// console.log('Document does not exist.')
// }
// })
// .catch((err) => {
// console.log(err.message)
// })
// Check if email id exists in user collection
function checkEmailInUserCol (email) {
const testDoc = doc(db, 'users', email);
return getDoc(testDoc)
.then((document) => {
return document.exists()
})
.catch((err) => {
console.log(err.message)
})
}
// Create new email id in user collection
function createEmailInUserCol (email, fname, lname) {
// const existingRecordFlag = checkEmailInUserCol(email)
checkEmailInUserCol(email)
.then((existingRecordFlag) => {
console.log('existingRecordFlag:', existingRecordFlag)
if ( !(existingRecordFlag) ) {
setDoc(doc(db, 'users', email), {
user_email: email,
firstname: fname,
lastname: lname,
photo: "",
bonus_tries: 0,
points: 0,
score: 0,
finished_tutorial: false,
})
.then(() => {
console.log('new user record created')
})
} else {
console.log('user already exists')
}
})
}
// Sign in with Facebook
const fbLoginButton = document.getElementById('fbLoginBtn');
const providerFB = new FacebookAuthProvider();
providerFB.addScope('email')
fbLoginButton.addEventListener('click', (e) => {
e.preventDefault();
signInWithPopup(auth, providerFB)
.then((cred) => {
console.log('Preparing users')
const user = cred.user;
console.log(user);
})
.catch((err) => {
console.log(err.message);
})
})
// Sign in with Google
const googleLoginButton = document.getElementById('googleLoginBtn');
const providerGoogle = new GoogleAuthProvider();
providerGoogle.addScope('https://www.googleapis.com/auth/userinfo.email');
providerGoogle.addScope('https://www.googleapis.com/auth/userinfo.profile');
googleLoginButton.addEventListener('click', (e) => {
e.preventDefault();
signInWithPopup(auth, providerGoogle)
.then((cred) => {
const user = cred.user.providerData[0];
// console.log(user)
createEmailInUserCol(user.email, user.displayName, "");
})
.catch((err) => {
console.log(err.message);
})
})