/
login.js
216 lines (205 loc) · 7.68 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// hide loading indicator.
hideLoading();
// get sign up container.
const signUpContainer = document.getElementById('signup');
// set up event for sign up close btn
const signUpCloseBtn = document.getElementById('signup__close-btn');
// set up event for create new account button.
const createNewAccountBtn = document.getElementById('login__create-account-btn');
// get input information from the input elements and validate those values.
const emailInputElement = document.getElementById('signup__email');
const passwordInputElement = document.getElementById('signup__password');
const confirmPasswordInputElement = document.getElementById('signup__confirm-password');
const emailLoginInputElement = document.getElementById('login__email');
const passwordLoginInputElement = document.getElementById('login__password');
// get sign up button.
const signUpBtn = document.getElementById('signup__btn');
// get login button.
const loginBtn = document.getElementById('login__submit-btn');
/**
* hide sing up modal.
*/
function hideSignUp() {
// add hide class to hide the sign up.
signUpContainer.classList.add('signup--hide');
// clear the input elements.
if (emailInputElement && passwordInputElement && confirmPasswordInputElement) {
emailInputElement.value = '';
passwordInputElement.value = '';
confirmPasswordInputElement.value = '';
}
}
// add event for sign up close button.
if (signUpCloseBtn) {
signUpCloseBtn.addEventListener('click', function() {
hideSignUp();
});
}
// add event for create a new account button.
if (createNewAccountBtn) {
createNewAccountBtn.addEventListener('click', function() {
signUpContainer.classList.remove('signup--hide');
});
}
/**
* validate input user's information when creating a new account.
* @param {*} object - user's information that needs to be validated.
* @returns valid, or not.
*/
function validateNewAccount({ email, password, confirmPassword }) {
if (!validator.isEmail(email)) {
alert("Please input your email");
return false;
}
if (validator.isEmpty(password) || !validator.isLength(password, {min: 6})) {
alert("Please input your password. You password must have at least 6 characters");
return false;
}
if (validator.isEmpty(confirmPassword)) {
alert("Please input your confirm password");
return false;
}
if (password !== confirmPassword) {
alert("Confirm password and password must be the same");
return false;
}
return true;
}
/**
* generate user's avatar
* @returns user's avatar.
*/
function generateAvatar() {
// hardcode list of user's avatars for the demo purpose.
const avatars= [
'https://data-us.cometchat.io/assets/images/avatars/captainamerica.png',
'https://data-us.cometchat.io/assets/images/avatars/cyclops.png',
'https://data-us.cometchat.io/assets/images/avatars/ironman.png',
'https://data-us.cometchat.io/assets/images/avatars/spiderman.png',
'https://data-us.cometchat.io/assets/images/avatars/wolverine.png'
];
const avatarPosition = Math.floor(Math.random() * avatars.length);
return avatars[avatarPosition];
}
/**
* register a new account - register with firebase and cometchat.
* @param {*} object - user's information that will be used to register.
*/
function registerNewAccount({email, password, confirmPassword}) {
if (validateNewAccount({email, password, confirmPassword})) {
// show loading indicator.
showLoading();
// get user avatar.
const userAvatar = generateAvatar();
// create new user's uuid.
const userUuid = uuid.v4();
// call firebase and cometchat service to register a new account.
auth.createUserWithEmailAndPassword(email, password).then((userCrendentials) => {
if (userCrendentials) {
// call firebase real time database to insert a new user.
realTimeDb.ref(`users/${userUuid}`).set({
id: userUuid,
email,
avatar: userAvatar
}).then(() => {
alert(`${userCrendentials.user.email} was created successfully! Please sign in with your created account`);
// call cometchat service to register a new account.
const user = new CometChatWidget.CometChat.User(userUuid);
user.setName(email);
user.setAvatar(userAvatar);
CometChatWidget.init({
"appID": `${config.CometChatAppId}`,
"appRegion": `${config.CometChatRegion}`,
"authKey": `${config.CometChatAuthKey}`
}).then(response => {
CometChatWidget.createOrUpdateUser(user).then(user => {
hideLoading();
} ,error => {
hideLoading();
});
hideSignUp();
}, error => {
//Check the reason for error and take appropriate action.
});
});
}
}).catch((error) => {
hideLoading();
alert(`Cannot create your account, ${email} might be existed, please try again!`);
});
}
}
// add event for sign up button.
if (signUpBtn) {
signUpBtn.addEventListener('click', function() {
if (emailInputElement && passwordInputElement && confirmPasswordInputElement) {
const email = emailInputElement.value;
const password = passwordInputElement.value;
const confirmPassword = confirmPasswordInputElement.value;
if (validateNewAccount({email, password, confirmPassword})) {
registerNewAccount({email, password, confirmPassword});
}
}
});
}
/**
* check user's credentials is valid, or not.
* @param {*} object - user's credentials.
* @returns valid, or not.
*/
function isUserCredentialsValid({email, password}) {
return email && password && validator.isEmail(email) && validator.isLength(password, {min: 6});
}
// add event for login button.
if (loginBtn) {
loginBtn.addEventListener('click', function() {
// show loading indicator.
showLoading();
// get input user's credentials.
const email = emailLoginInputElement ? emailLoginInputElement.value : null;
const password = passwordLoginInputElement ? passwordLoginInputElement.value : null;
if(isUserCredentialsValid({email, password})) {
// if the user's credentials are valid, call Firebase authentication service.
auth.signInWithEmailAndPassword(email, password).then((userCredential) => {
const userEmail = userCredential.user.email;
realTimeDb.ref().child('users').orderByChild('email').equalTo(userEmail).on("value", function(snapshot) {
const val = snapshot.val();
if (val) {
const keys = Object.keys(val);
const user = val[keys[0]];
if (user && user.id) {
CometChatWidget.init({
"appID": `${config.CometChatAppId}`,
"appRegion": `${config.CometChatRegion}`,
"authKey": `${config.CometChatAuthKey}`
}).then(response => {
CometChatWidget.login({uid: user.id}).then((loggedInUser) => {
// User loged in successfully.
// hide loading.
hideLoading();
// redirect to home page.
window.location.href = '/';
});
}, error => {
//Check the reason for error and take appropriate action.
});
} else {
// hide loading indicator.
hideLoading();
alert(`Your user's name or password is not correct`);
}
}
});
})
.catch((error) => {
// hide loading indicator.
hideLoading();
alert(`Your user's name or password is not correct`);
});
} else {
// hide loading indicator.
hideLoading();
alert(`Your user's name or password is not correct`);
}
});
}