-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.js
More file actions
388 lines (369 loc) · 16 KB
/
callback.js
File metadata and controls
388 lines (369 loc) · 16 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
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
/*
* 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 Callback handler.
*/
goog.provide('firebaseui.auth.widget.handler.handleCallback');
goog.require('firebaseui.auth.PendingEmailCredential');
goog.require('firebaseui.auth.idp');
goog.require('firebaseui.auth.soy2.strings');
goog.require('firebaseui.auth.storage');
goog.require('firebaseui.auth.ui.page.Callback');
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.require('goog.array');
/**
* Handles the IDP callback.
*
* @param {!firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {Element} container The container DOM element.
* @param {?goog.Promise<!firebase.auth.UserCredential>=} opt_result
* UserCredential from a redirect or popup sign in attempt.
*/
firebaseui.auth.widget.handler.handleCallback =
function(app, container, opt_result) {
// Render the UI.
var component = new firebaseui.auth.ui.page.Callback();
component.render(container);
// Set current UI component.
app.setCurrentComponent(component);
// Get result either from passed result or from app's getRedirectResult.
var resultObtainer = opt_result || app.getRedirectResult();
app.registerPending(resultObtainer.then(function(result) {
firebaseui.auth.widget.handler.handleCallbackResult_(app, component,
result);
}, function(error) {
// A previous redirect operation was triggered and some error occurred.
// Test for need confirmation error and handle appropriately.
// For all other errors, display info bar and show sign in screen.
if (error &&
// Single out need confirmation error as email-already-in-use and
// credential-already-in-use will also return email and credential
// and need to be handled differently.
(error['code'] == 'auth/account-exists-with-different-credential' ||
error['code'] == 'auth/email-already-in-use') &&
error['email'] &&
error['credential']) {
// Save pending email credential.
firebaseui.auth.storage.setPendingEmailCredential(
new firebaseui.auth.PendingEmailCredential(
error['email'], error['credential']),
app.getAppId());
firebaseui.auth.widget.handler.handleCallbackLinking_(
app, component, error['email']);
} else if (error && error['code'] == 'auth/user-cancelled') {
// Should go back to the previous linking screen. A pending email
// should be present, otherwise there's an error.
var pendingCredential =
firebaseui.auth.storage.getPendingEmailCredential(app.getAppId());
var message =
firebaseui.auth.widget.handler.common.getErrorMessage(error);
// If there is a credential too, then the previous screen was federated
// linking so we process the error as a linking flow.
if (pendingCredential && pendingCredential.getCredential()) {
firebaseui.auth.widget.handler.handleCallbackLinking_(
app, component, pendingCredential.getEmail(), message);
// Otherwise, the user had entered his email but a federated account
// already existed. It had then triggered federated sign in, but the user
// did not consent to the scopes. It then needs to restart the federated
// sign in flow.
} else if (pendingCredential) {
firebaseui.auth.widget.handler.common.handleStartEmailFirstFlow(
app, component, pendingCredential.getEmail(), message);
} else {
// Go to the sign-in page with info bar error.
firebaseui.auth.widget.handler.handleCallbackFailure_(
app, component, /** @type {!Error} */ (error));
}
} else if (error && error['code'] == 'auth/credential-already-in-use') {
// Do nothing and keep callback UI while onUpgradeError catches and
// handles this error.
} else if (error &&
error['code'] == 'auth/operation-not-supported-in-this-environment' &&
firebaseui.auth.widget.handler.common.isPasswordProviderOnly(app)) {
// Operation is not supported in this environment but only password
// provider is enabled. So allow this to proceed as a no redirect result.
// This will allow developers using password sign-in in Cordova to use
// FirebaseUI.
firebaseui.auth.widget.handler.handleCallbackResult_(
app,
component,
{
'user': null,
'credential': null
});
} else {
// Go to the sign-in page with info bar error.
firebaseui.auth.widget.handler.handleCallbackFailure_(
app, component, /** @type {!Error} */ (error));
}
}));
};
/**
* Handles callback when the getRedirectResult is successful.
* @param {!firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {!firebaseui.auth.ui.page.Base} component The current UI component if
* present.
* @param {!firebase.auth.UserCredential} result The result from the
* getRedirectResult call.
* @private
*/
firebaseui.auth.widget.handler.handleCallbackResult_ =
function(app, component, result) {
if (result['user']) {
var authResult = /** @type {!firebaseui.auth.AuthResult} */ ({
'user': result['user'],
'credential': result['credential'],
'operationType': result['operationType'],
'additionalUserInfo': result['additionalUserInfo']
});
// Sign in or link with redirect was previously triggered.
var pendingEmailCredential =
firebaseui.auth.storage.getPendingEmailCredential(app.getAppId());
// The email originally used before the federated sign in, if any.
var pendingEmail =
pendingEmailCredential && pendingEmailCredential.getEmail();
// Test for email mismatch cases.
if (pendingEmail &&
!firebaseui.auth.widget.handler.hasUserEmailAddress_(
result['user'], pendingEmail)) {
// The user tried originally to sign in with a different
// email than the one coming from the provider.
firebaseui.auth.widget.handler.handleCallbackEmailMismatch_(
app, component, authResult);
return;
}
var pendingCredential =
pendingEmailCredential && pendingEmailCredential.getCredential();
if (pendingCredential) {
// Check if there is a pending auth credential. If so, complete the link
// process and delete the pending credential.
app.registerPending(result['user'].linkWithCredential(
pendingCredential)
.then(function(userCredential) {
// Linking successful, complete sign in, pass pending credentials
// as the developer originally expected them in the sign in
// attempt that triggered the link.
authResult = /** @type {!firebaseui.auth.AuthResult} */ ({
'user': userCredential['user'],
'credential': pendingCredential,
// Even though the operation type returned here is always 'link',
// we will sign in again on external Auth instance with this
// credential returning 'signIn' or 'link' in case of anonymous
// upgrade through finishSignInAndRetrieveDataWithAuthResult.
'operationType': userCredential['operationType'],
'additionalUserInfo': userCredential['additionalUserInfo']
});
firebaseui.auth.widget.handler.handleCallbackSuccess_(
app, component, authResult);
},
function(error) {
// Go to the sign-in page with info bar error.
firebaseui.auth.widget.handler.handleCallbackFailure_(
app, component, error);
}));
} else {
// No pending credential, complete sign in.
firebaseui.auth.widget.handler.handleCallbackSuccess_(
app, component, authResult);
}
} else {
// No previous redirect operation, go back to the sign-in page with no
// error.
var container = component.getContainer();
component.dispose();
// Clean the pending email credential, if any, to avoid keeping track of
// a linking flow after the user has refreshed the page (i.e. when no more
// redirect result).
firebaseui.auth.storage.removePendingEmailCredential(app.getAppId());
firebaseui.auth.widget.handler.common.handleSignInStart(app, container);
}
};
/**
* Handles callback success.
* @param {!firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {!firebaseui.auth.ui.page.Base} component The current UI component if
* present.
* @param {!firebaseui.auth.AuthResult} authResult The Auth result, which
* includes current user, credential to sign in on external Auth instance,
* additional user info and operation type.
* @private
*/
firebaseui.auth.widget.handler.handleCallbackSuccess_ =
function(app, component, authResult) {
firebaseui.auth.storage.removePendingEmailCredential(app.getAppId());
firebaseui.auth.widget.handler.common.setLoggedInWithAuthResult(
app, component, authResult);
};
/**
* Handles callback failure.
* @param {!firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {!firebaseui.auth.ui.page.Base} component The current UI component if
* present.
* @param {!Error} error The error that caused the failure.
* @private
*/
firebaseui.auth.widget.handler.handleCallbackFailure_ =
function(app, component, error) {
var container = component.getContainer();
firebaseui.auth.storage.removePendingEmailCredential(app.getAppId());
var errorMessage =
firebaseui.auth.widget.handler.common.getErrorMessage(error);
component.dispose();
// Call widget sign in start handler.
firebaseui.auth.widget.handler.common.handleSignInStart(
app, container, undefined, errorMessage);
};
/**
* Handles callback linking required, fetching the available sign in methods for
* the user's email and using the correct handler based on the recommended
* sign in method.
* @param {!firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {!firebaseui.auth.ui.page.Base} component The current UI component.
* @param {string} email The user's email.
* @param {string=} opt_infoBarMessage The message to show on info bar.
* @private
*/
firebaseui.auth.widget.handler.handleCallbackLinking_ =
function(app, component, email, opt_infoBarMessage) {
var container = component.getContainer();
app.registerPending(app.getAuth().fetchSignInMethodsForEmail(email)
.then(function(signInMethods) {
component.dispose();
if (!signInMethods.length) {
// No ability to link. Clear pending email credential.
firebaseui.auth.storage.removePendingEmailCredential(app.getAppId());
// Edge case scenario: anonymous account exists with the current
// email. Linking will be required and providers array will be empty.
// Provide a way for user to recover. Only way is via password reset.
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.PASSWORD_RECOVERY,
app,
container,
email,
// Allow the user to cancel.
false,
// Display a message to explain to the user what happened.
firebaseui.auth.soy2.strings.errorAnonymousEmailBlockingSignIn()
.toString());
} else if (goog.array.contains(signInMethods,
firebase.auth.EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD)) {
// In this scenario, there can't be any error message passed from a
// auth/user-cancelled error, as the sign in method is password.
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.PASSWORD_LINKING,
app,
container,
email);
} else if (signInMethods.length == 1 && signInMethods[0] ===
firebase.auth.EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD) {
// In this scenario, there can't be any error message passed from a
// auth/user-cancelled error, as the sign in method is email link.
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.EMAIL_LINK_SIGN_IN_LINKING,
app,
container,
email);
} else {
var federatedSignInMethod =
firebaseui.auth.idp.getFirstFederatedSignInMethod(
signInMethods, app.getConfig().getProviders());
if (federatedSignInMethod) {
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.FEDERATED_LINKING,
app,
container,
email,
federatedSignInMethod,
opt_infoBarMessage);
} else {
// No ability to link. Clear pending email credential.
firebaseui.auth.storage.removePendingEmailCredential(
app.getAppId());
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.UNSUPPORTED_PROVIDER,
app,
container,
email);
}
}
}, function(error) {
firebaseui.auth.widget.handler.handleCallbackFailure_(
app, component, /** @type {!Error} */ (error));
}));
};
/**
* Handles email mismatch. Calls the email mismatch handler.
* @param {!firebaseui.auth.AuthUI} app The current FirebaseUI instance whose
* configuration is used.
* @param {!firebaseui.auth.ui.page.Base} component The current UI component.
* @param {!firebaseui.auth.AuthResult} authResult The Auth result object.
* @private
*/
firebaseui.auth.widget.handler.handleCallbackEmailMismatch_ =
function(app, component, authResult) {
var container = component.getContainer();
// On email mismatch, sign out the temporary user to avoid leaking this
// temp auth session if the user decides to close the window.
app.registerPending(app.clearTempAuthState().then(function() {
component.dispose();
firebaseui.auth.widget.handler.handle(
firebaseui.auth.widget.HandlerName.EMAIL_MISMATCH,
app,
container,
authResult);
}, function(error) {
// Ignore error if cancelled by the client.
if (error['name'] && error['name'] == 'cancel') {
return;
}
var errorMessage = firebaseui.auth.widget.handler.common.getErrorMessage(
error['code']);
component.showInfoBar(errorMessage);
}));
};
/**
* Tests whether the email address given is one of the user's email addresses.
* @param {!firebase.User} user The user whose email addresses we're testing.
* @param {string} email The email address to test.
* @return {boolean}
* @private
*/
firebaseui.auth.widget.handler.hasUserEmailAddress_ = function(user, email) {
if (email == user['email']) {
return true;
}
// Tests provider's email addresses.
if (user['providerData']) {
for (var i = 0; i < user['providerData'].length; i++) {
var provider = user['providerData'][i];
if (email == provider['email']) {
return true;
}
}
}
return false;
};
// Register handler.
firebaseui.auth.widget.handler.register(
firebaseui.auth.widget.HandlerName.CALLBACK,
/** @type {firebaseui.auth.widget.Handler} */
(firebaseui.auth.widget.handler.handleCallback));