-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathactioncode.js
441 lines (412 loc) · 16.8 KB
/
actioncode.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Handlers for action code.
*/
goog.provide('firebaseui.auth.widget.handler.handleEmailChangeRevocation');
goog.provide('firebaseui.auth.widget.handler.handleEmailVerification');
goog.provide('firebaseui.auth.widget.handler.handlePasswordReset');
goog.provide('firebaseui.auth.widget.handler.handleRevertSecondFactorAddition');
goog.provide('firebaseui.auth.widget.handler.handleVerifyAndChangeEmail');
goog.require('firebaseui.auth.soy2.strings');
goog.require('firebaseui.auth.ui.element');
goog.require('firebaseui.auth.ui.page.EmailChangeRevoke');
goog.require('firebaseui.auth.ui.page.EmailChangeRevokeFailure');
goog.require('firebaseui.auth.ui.page.EmailVerificationFailure');
goog.require('firebaseui.auth.ui.page.EmailVerificationSuccess');
goog.require('firebaseui.auth.ui.page.PasswordRecoveryEmailSent');
goog.require('firebaseui.auth.ui.page.PasswordReset');
goog.require('firebaseui.auth.ui.page.PasswordResetFailure');
goog.require('firebaseui.auth.ui.page.PasswordResetSuccess');
goog.require('firebaseui.auth.ui.page.RevertSecondFactorAdditionFailure');
goog.require('firebaseui.auth.ui.page.RevertSecondFactorAdditionSuccess');
goog.require('firebaseui.auth.ui.page.VerifyAndChangeEmailFailure');
goog.require('firebaseui.auth.ui.page.VerifyAndChangeEmailSuccess');
goog.require('firebaseui.auth.widget.Handler');
goog.require('firebaseui.auth.widget.HandlerName');
goog.require('firebaseui.auth.widget.handler');
goog.require('firebaseui.auth.widget.handler.common');
goog.requireType('goog.Promise');
/**
* Handles password reset.
*
* @param {firebaseui.auth.AuthUI} app The current Firebase UI instance whose
* configuration is used.
* @param {Element} container The container DOM element.
* @param {string} actionCode The password reset action code.
* @param {?function()=} opt_onContinueClick Callback to invoke when the
* continue button is clicked. If not provided, no continue button is
* displayed.
*/
firebaseui.auth.widget.handler.handlePasswordReset = function(
app, container, actionCode, opt_onContinueClick) {
// Call resetPassword first to get the email address.
app.registerPending(
app.getAuth()
.verifyPasswordResetCode(actionCode)
.then(
function(email) {
// Show reset password UI.
var component = new firebaseui.auth.ui.page.PasswordReset(
email, function() {
firebaseui.auth.widget.handler.resetPassword_(
app, container, component, actionCode,
opt_onContinueClick);
});
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
},
function(error) {
firebaseui.auth.widget.handler.handlePasswordResetFailure_(
app, container);
}));
};
/**
* @param {firebaseui.auth.AuthUI} app The current Firebase UI instance whose
* configuration is used.
* @param {Element} container The container DOM element.
* @param {firebaseui.auth.ui.page.PasswordReset} component The UI component.
* @param {string} actionCode The password reset action code.
* @param {?function()=} opt_onContinueClick Callback to invoke when the
* continue button is clicked. If not provided, no continue button is
* displayed.
* @private
*/
firebaseui.auth.widget.handler.resetPassword_ = function(
app, container, component, actionCode, opt_onContinueClick) {
var newPassword = component.checkAndGetNewPassword();
if (!newPassword) {
return;
}
var currentComponent = component;
app.registerPending(component.executePromiseRequest(
/** @type {function (): !goog.Promise} */ (
goog.bind(app.getAuth().confirmPasswordReset, app.getAuth())),
[actionCode, newPassword],
function(resp) {
currentComponent.dispose();
var component = new firebaseui.auth.ui.page.PasswordResetSuccess(
opt_onContinueClick);
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
},
function(error) {
// Sign out user and show password reset failure.
firebaseui.auth.widget.handler.handlePasswordResetFailure_(
app, container, component, /** @type {Error} */ (error));
}));
};
/**
* @param {firebaseui.auth.AuthUI} app The current Firebase UI instance whose
* configuration is used.
* @param {Element} container The container DOM element.
* @param {firebaseui.auth.ui.page.PasswordReset=} opt_component Current UI
* component.
* @param {Error=} opt_error The error received from the backend.
* @private
*/
firebaseui.auth.widget.handler.handlePasswordResetFailure_ = function(
app, container, opt_component, opt_error) {
const errorCode = opt_error && opt_error['code'];
if (errorCode === 'auth/weak-password') {
// Handles this error differently as it just requires to display a message
// to the user to use a longer password.
const errorMessage =
firebaseui.auth.widget.handler.common.getErrorMessage(opt_error);
firebaseui.auth.ui.element.setValid(
opt_component.getNewPasswordElement(), false);
firebaseui.auth.ui.element.show(
opt_component.getNewPasswordErrorElement(), errorMessage);
opt_component.getNewPasswordElement().focus();
} else if (errorCode === 'auth/password-does-not-meet-requirements') {
// Pass the error message from the backend which contains all the password
// requirements to be met.
const errorMessage =
firebaseui.auth.widget.handler.common.getErrorMessage(opt_error);
firebaseui.auth.ui.element.setValid(
opt_component.getNewPasswordElement(), false);
firebaseui.auth.ui.element.show(
opt_component.getNewPasswordErrorElement(), errorMessage);
opt_component.getNewPasswordElement().focus();
} else {
if (opt_component) {
opt_component.dispose();
}
var component = new firebaseui.auth.ui.page.PasswordResetFailure();
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
}
};
/**
* Handles email change revocation action code.
*
* @param {firebaseui.auth.AuthUI} app The current Firebase UI instance whose
* configuration is used.
* @param {Element} container The container DOM element.
* @param {string} actionCode The new email verification action code.
*/
firebaseui.auth.widget.handler.handleEmailChangeRevocation = function(
app, container, actionCode) {
var email = null;
// Gets the email related to the code.
app.registerPending(
app.getAuth()
.checkActionCode(actionCode)
.then(function(info) {
email = info['data']['email'];
// Then applies it.
return app.getAuth().applyActionCode(actionCode);
})
.then(
function() {
firebaseui.auth.widget.handler
.handleEmailChangeRevocationSuccess_(app, container, email);
},
function(error) {
firebaseui.auth.widget.handler
.handleEmailChangeRevocationFailure_(app, container);
}));
};
/**
* Handles email change revocation success.
*
* @param {firebaseui.auth.AuthUI} app The current Firebase UI instance whose
* configuration is used.
* @param {Element} container The container DOM element.
* @param {string} email The old email to revert to.
* @private
*/
firebaseui.auth.widget.handler.handleEmailChangeRevocationSuccess_ =
function(app, container, email) {
var component = new firebaseui.auth.ui.page.EmailChangeRevoke(
email,
function () {
app.registerPending(component.executePromiseRequest(
/** @type {function (): !goog.Promise} */ (
goog.bind(app.getAuth().sendPasswordResetEmail, app.getAuth())),
[email],
function() {
// Reset password code sent.
component.dispose();
component =
new firebaseui.auth.ui.page.PasswordRecoveryEmailSent(
email,
undefined,
app.getConfig().getTosUrl(),
app.getConfig().getPrivacyPolicyUrl());
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
}, function(error) {
// Failed to send reset password code.
component.showInfoBar(
firebaseui.auth.soy2.strings.errorSendPasswordReset()
.toString());
}));
});
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
};
/**
* Handles email change revocation failure.
*
* @param {firebaseui.auth.AuthUI} app The current Firebase UI instance whose
* configuration is used.
* @param {Element} container The container DOM element.
* @private
*/
firebaseui.auth.widget.handler.handleEmailChangeRevocationFailure_ =
function(app, container) {
var component = new firebaseui.auth.ui.page.EmailChangeRevokeFailure();
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
};
/**
* Handles email verification action code.
*
* @param {firebaseui.auth.AuthUI} app The current Firebase UI instance whose
* configuration is used.
* @param {Element} container The container DOM element.
* @param {string} actionCode The email verification action code.
* @param {?function()=} opt_onContinueClick Callback to invoke when the
* continue button is clicked. If not provided, no continue button is
* displayed.
*/
firebaseui.auth.widget.handler.handleEmailVerification = function(
app, container, actionCode, opt_onContinueClick) {
app.registerPending(
app.getAuth()
.applyActionCode(actionCode)
.then(
function() {
var component =
new firebaseui.auth.ui.page.EmailVerificationSuccess(
opt_onContinueClick);
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
},
function(error) {
var component =
new firebaseui.auth.ui.page.EmailVerificationFailure();
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
}));
};
/**
* Handles the verify and change email action flow.
*
* @param {!firebaseui.auth.AuthUI} app The current Firebase UI instance whose
* configuration is used.
* @param {!Element} container The container DOM element.
* @param {string} actionCode The verify and change email action code.
* @param {?function()=} onContinueClick The optional callback to invoke when
* the continue button is clicked. If not provided, no continue button is
* displayed.
*/
firebaseui.auth.widget.handler.handleVerifyAndChangeEmail = function(
app, container, actionCode, onContinueClick) {
let email = null;
// Gets the email related to the code.
app.registerPending(
app.getAuth()
.checkActionCode(actionCode)
.then((info) => {
email = info['data']['email'];
// Then applies it.
return app.getAuth().applyActionCode(actionCode);
})
.then(
() => {
const component =
new firebaseui.auth.ui.page.VerifyAndChangeEmailSuccess(
email, onContinueClick);
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
},
(error) => {
const component =
new firebaseui.auth.ui.page.VerifyAndChangeEmailFailure();
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
}));
};
/**
* Handles the revert second factor addition email action flow.
*
* @param {!firebaseui.auth.AuthUI} app The current Firebase UI instance whose
* configuration is used.
* @param {!Element} container The container DOM element.
* @param {string} actionCode The revert second factor addition action code.
*/
firebaseui.auth.widget.handler.handleRevertSecondFactorAddition =
function(app, container, actionCode) {
let email = null;
let multiFactorInfo = null;
app.registerPending(
app.getAuth()
.checkActionCode(actionCode)
.then((info) => {
email = info['data']['email'];
multiFactorInfo = info['data']['multiFactorInfo'];
// Then applies it.
return app.getAuth().applyActionCode(actionCode);
})
.then(() => {
firebaseui.auth.widget.handler
.handleRevertSecondFactorAdditionSuccess_(
app, container, email, multiFactorInfo);
}, (error) => {
const component =
new firebaseui.auth.ui.page
.RevertSecondFactorAdditionFailure();
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
}));
};
/**
* Handles the successful revert second factor addition action.
* @param {!firebaseui.auth.AuthUI} app The current Firebase UI instance whose
* configuration is used.
* @param {!Element} container The container DOM element.
* @param {string} email The email of the acount.
* @param {!firebase.auth.MultiFactorInfo} multiFactorInfo The info of
* multi-factor to be unenrolled.
* @private
*/
firebaseui.auth.widget.handler.handleRevertSecondFactorAdditionSuccess_ =
function(app, container, email, multiFactorInfo) {
let component = new firebaseui.auth.ui.page.RevertSecondFactorAdditionSuccess(
multiFactorInfo['factorId'],
() => {
component.executePromiseRequest(
goog.bind(app.getAuth().sendPasswordResetEmail, app.getAuth()),
[email],
() => {
// Reset password code sent.
component.dispose();
component =
new firebaseui.auth.ui.page.PasswordRecoveryEmailSent(
email,
undefined,
app.getConfig().getTosUrl(),
app.getConfig().getPrivacyPolicyUrl());
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
}, (error) => {
// Failed to send reset password code.
component.showInfoBar(
firebaseui.auth.soy2.strings.errorSendPasswordReset()
.toString());
});
},
multiFactorInfo['phoneNumber']);
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
};
// Register handlers.
firebaseui.auth.widget.handler.register(
firebaseui.auth.widget.HandlerName.PASSWORD_RESET,
/** @type {firebaseui.auth.widget.Handler} */
(firebaseui.auth.widget.handler.handlePasswordReset));
firebaseui.auth.widget.handler.register(
firebaseui.auth.widget.HandlerName.EMAIL_CHANGE_REVOCATION,
/** @type {firebaseui.auth.widget.Handler} */
(firebaseui.auth.widget.handler.handleEmailChangeRevocation));
/** @suppress {missingRequire} */
firebaseui.auth.widget.handler.register(
firebaseui.auth.widget.HandlerName.EMAIL_VERIFICATION,
/** @type {firebaseui.auth.widget.Handler} */
(firebaseui.auth.widget.handler.handleEmailVerification));
/** @suppress {missingRequire} */
firebaseui.auth.widget.handler.register(
firebaseui.auth.widget.HandlerName.REVERT_SECOND_FACTOR_ADDITION,
/** @type {!firebaseui.auth.widget.Handler} */
(firebaseui.auth.widget.handler.handleRevertSecondFactorAddition));
/** @suppress {missingRequire} */
firebaseui.auth.widget.handler.register(
firebaseui.auth.widget.HandlerName.VERIFY_AND_CHANGE_EMAIL,
/** @type {!firebaseui.auth.widget.Handler} */
(firebaseui.auth.widget.handler.handleVerifyAndChangeEmail));