-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserManagement.js
More file actions
167 lines (156 loc) · 5.62 KB
/
userManagement.js
File metadata and controls
167 lines (156 loc) · 5.62 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
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
import { create } from "qrcode";
function getParameterByName(name) {
const paramsObj = getParamsObject();
return paramsObj[name];
}
// Get the action to complete
const mode = getParameterByName('mode');
const actionCode = getParameterByName('oobCode');
const continueUrl = getParameterByName('continueUrl');
const lang = getParameterByName('lang');
var accountEmail;
// Handle the user management action.
switch (mode) {
case 'resetPassword':
// Display reset password handler and UI.
handleResetPassword(auth, actionCode, continueUrl, lang);
break;
case 'recoverEmail':
// Display email recovery handler and UI.
handleRecoverEmail(auth, actionCode, lang);
break;
case 'verifyEmail':
// Display email verification handler and UI.
handleVerifyEmail(auth, actionCode, continueUrl, lang);
break;
case 'sendEmailVerification':
// Display email verification handler and UI.
sendEmailVerification(auth);
break;
default:
// Error: invalid mode.
}
// [START auth_handle_reset_password]
function handleResetPassword(auth, actionCode, continueUrl, lang) {
// Localize the UI to the selected language as determined by the lang
// parameter.
// Verify the password reset code is valid.
auth.verifyPasswordResetCode(actionCode).then((email) => {
accountEmail = email;
accountEmailText.innerHTML = `Email: ${accountEmail}`;
resetPasswordDiv.style.display = 'block';
loadingDiv.style.display = 'none';
}).catch((error) => {
console.log(error.code, error.message);
errorHandler.report(error);
});
}
// [END auth_handle_reset_password]
// [START auth_handle_recover_email]
function handleRecoverEmail(auth, actionCode, lang) {
// Localize the UI to the selected language as determined by the lang
// parameter.
var restoredEmail = null;
// Confirm the action code is valid.
auth.checkActionCode(actionCode).then((info) => {
// Get the restored email address.
restoredEmail = info['data']['email'];
// Revert to the old email.
return auth.applyActionCode(actionCode);
}).then(() => {
// Account email reverted to restoredEmail
// TODO: Display a confirmation message to the user.
// You might also want to give the user the option to reset their password
// in case the account was compromised:
auth.sendPasswordResetEmail(restoredEmail).then(() => {
// Password reset confirmation sent. Ask user to check their email.
}).catch((error) => {
errorHandler.report(error);
// Error encountered while sending password reset code.
});
}).catch((error) => {
errorHandler.report(error);
// Invalid code.
});
}
// [END auth_handle_recover_email]
// [START auth_handle_verify_email]
function handleVerifyEmail(auth, actionCode, continueUrl, lang) {
// Localize the UI to the selected language as determined by the lang
// parameter.
// Try to apply the email verification code.
auth.applyActionCode(actionCode).then((resp) => {
// Email address has been verified.
// Show confirmation.
emailVerifiedDiv.style.display = 'block';
loadingDiv.style.display = 'none';
toMaiButton.style.display = 'block';
}).catch((error) => {
errorHandler.report(error);
emailVerificationFailedDiv.style.display = 'block';
loadingDiv.style.display = 'none';
// Code is invalid or expired. Ask the user to verify their email address
// again.
});
}
// [END auth_handle_verify_email]
function sendEmailVerification(auth) {
authUser.whenSet((user) => {
if (user) {
emailVerificationText.innerHTML = `Klicka på länken i mailet vi skickat till ${user.email} för att verifiera din email.`;
}
verifyEmailDiv.style.display = 'block';
loadingDiv.style.display = 'none';
setCookie('viewedVerifyEmailDiv', 'true', 2);
});
}
sendEmailVerificationButton.addEventListener('click', function sendEmail() {
firebase.auth().currentUser.sendEmailVerification()
.then(() => {
// Email verification sent!
emailSentDiv.style.display = 'block';
sendEmailVerificationButton.style.display = 'none';
setTimeout(function showButton() {
toMaiButton.style.display = 'block';
closeVerifyEmailButton.style.display = 'none';
}, 10000);
});
});
savePasswordAndLoginButton.addEventListener('click', function () {
// Save the new password.
passwordLoadIcon.style.display = 'block';
savePasswordAndLoginButton.style.display = 'none';
var password = newPassword.value;
auth.confirmPasswordReset(actionCode, password).then((resp) => {
console.log("Password reset has been confirmed and new password updated");
// Sign in
firebase.auth().signInWithEmailAndPassword(accountEmail, password)
.then((userCredential) => {
if (continueUrl) {
let redirectUrl = continueUrl;
try {
redirectUrl = decodeURIComponent(continueUrl);
} catch (e) {
// decodeURIComponent may throw, so we catch and fall back to original string.
redirectUrl = continueUrl;
}
if (createCrossDomainSession) {
createCrossDomainSession().then(() => {
location.href = redirectUrl;
});
} else {
location.href = redirectUrl.startsWith('https://mairesale.com') ? 'https://mairesale.com/account' : redirectUrl;
}
} else {
location.href = './private';
}
})
.catch((error) => {
errorHandler.report(error);
console.log("Error message:", error.code, error.message);
});
}).catch((error) => {
errorHandler.report(error);
console.log("Error message:", error.code, error.message);
});
});