-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
firebase_auth_web.dart
506 lines (447 loc) 路 14.1 KB
/
firebase_auth_web.dart
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// ignore_for_file: require_trailing_commas
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart';
import 'package:firebase_auth_web/src/firebase_auth_web_multi_factor.dart';
import 'package:firebase_auth_web/src/interop/utils/utils.dart';
import 'package:firebase_auth_web/src/utils/web_utils.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_core_web/firebase_core_web.dart';
import 'package:firebase_core_web/firebase_core_web_interop.dart'
as core_interop;
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'src/firebase_auth_web_confirmation_result.dart';
import 'src/firebase_auth_web_recaptcha_verifier_factory.dart';
import 'src/firebase_auth_web_user.dart';
import 'src/firebase_auth_web_user_credential.dart';
import 'src/interop/auth.dart' as auth_interop;
import 'src/interop/multi_factor.dart' as multi_factor;
/// The web delegate implementation for [FirebaseAuth].
class FirebaseAuthWeb extends FirebaseAuthPlatform {
/// Stub initializer to allow the [registerWith] to create an instance without
/// registering the web delegates or listeners.
FirebaseAuthWeb._()
: _webAuth = null,
super(appInstance: null);
Completer<void> _initialized = Completer();
// To set "persistence" on web, it is now required on the v9.0.0 or above Firebase JS SDK to pass the value on calling `initializeAuth()`.
// https://firebase.google.com/docs/reference/js/auth.md#initializeauth
Persistence? _persistence;
/// The entry point for the [FirebaseAuthWeb] class.
FirebaseAuthWeb({required FirebaseApp app, Persistence? persistence})
: super(appInstance: app) {
_persistence = persistence;
// Create a app instance broadcast stream for both delegate listener events
_userChangesListeners[app.name] =
StreamController<UserPlatform?>.broadcast();
_authStateChangesListeners[app.name] =
StreamController<UserPlatform?>.broadcast();
_idTokenChangesListeners[app.name] =
StreamController<UserPlatform?>.broadcast();
// TODO(rrousselGit): close StreamSubscription
delegate.onAuthStateChanged.map((auth_interop.User? webUser) {
if (!_initialized.isCompleted) {
_initialized.complete();
}
if (webUser == null) {
return null;
} else {
return UserWeb(
this,
MultiFactorWeb(this, multi_factor.multiFactor(webUser)),
webUser,
_webAuth,
);
}
}).listen((UserWeb? webUser) {
_authStateChangesListeners[app.name]!.add(webUser);
});
// TODO(rrousselGit): close StreamSubscription
// Also triggers `userChanged` events
delegate.onIdTokenChanged.map((auth_interop.User? webUser) {
if (webUser == null) {
return null;
} else {
return UserWeb(
this,
MultiFactorWeb(this, multi_factor.multiFactor(webUser)),
webUser,
_webAuth,
);
}
}).listen((UserWeb? webUser) {
_idTokenChangesListeners[app.name]!.add(webUser);
_userChangesListeners[app.name]!.add(webUser);
});
}
/// Called by PluginRegistry to register this plugin for Flutter Web
static void registerWith(Registrar registrar) {
FirebaseCoreWeb.registerService('auth', () async {
await FirebaseAuthWeb.instance.delegate.onWaitInitState();
});
FirebaseAuthPlatform.instance = FirebaseAuthWeb.instance;
PhoneMultiFactorGeneratorPlatform.instance = PhoneMultiFactorGeneratorWeb();
RecaptchaVerifierFactoryPlatform.instance =
RecaptchaVerifierFactoryWeb.instance;
}
static Map<String, StreamController<UserPlatform?>>
_authStateChangesListeners = <String, StreamController<UserPlatform?>>{};
static Map<String, StreamController<UserPlatform?>> _idTokenChangesListeners =
<String, StreamController<UserPlatform?>>{};
static Map<String, StreamController<UserPlatform?>> _userChangesListeners =
<String, StreamController<UserPlatform?>>{};
/// Initializes a stub instance to allow the class to be registered.
static FirebaseAuthWeb get instance {
return FirebaseAuthWeb._();
}
/// instance of Auth from the web plugin
auth_interop.Auth? _webAuth;
auth_interop.Auth get delegate {
_webAuth ??= auth_interop.getAuthInstance(core_interop.app(app.name),
persistence: _persistence);
return _webAuth!;
}
@override
FirebaseAuthPlatform delegateFor(
{required FirebaseApp app, Persistence? persistence}) {
return FirebaseAuthWeb(app: app, persistence: persistence);
}
@override
FirebaseAuthWeb setInitialValues({
Map<String, dynamic>? currentUser,
String? languageCode,
}) {
// Values are already set on web
return this;
}
@override
UserPlatform? get currentUser {
auth_interop.User? webCurrentUser = delegate.currentUser;
if (webCurrentUser == null) {
return null;
}
return UserWeb(
this,
MultiFactorWeb(this, multi_factor.multiFactor(delegate.currentUser!)),
delegate.currentUser!,
_webAuth,
);
}
@override
String? get tenantId {
return delegate.tenantId;
}
@override
set tenantId(String? tenantId) {
delegate.tenantId = tenantId;
}
@override
void sendAuthChangesEvent(String appName, UserPlatform? userPlatform) {
assert(_userChangesListeners[appName] != null);
_userChangesListeners[appName]!.add(userPlatform);
}
@override
Future<void> applyActionCode(String code) async {
try {
await delegate.applyActionCode(code);
} catch (e) {
throw getFirebaseAuthException(e);
}
}
@override
Future<ActionCodeInfo> checkActionCode(String code) async {
try {
return convertWebActionCodeInfo(await delegate.checkActionCode(code))!;
} catch (e) {
throw getFirebaseAuthException(e);
}
}
@override
Future<void> confirmPasswordReset(String code, String newPassword) async {
try {
await delegate.confirmPasswordReset(code, newPassword);
} catch (e) {
throw getFirebaseAuthException(e);
}
}
@override
Future<UserCredentialPlatform> createUserWithEmailAndPassword(
String email, String password) async {
try {
return UserCredentialWeb(
this,
await delegate.createUserWithEmailAndPassword(email, password),
_webAuth,
);
} catch (e) {
throw getFirebaseAuthException(e);
}
}
@override
Future<List<String>> fetchSignInMethodsForEmail(String email) async {
try {
return await delegate.fetchSignInMethodsForEmail(email);
} catch (e) {
throw getFirebaseAuthException(e);
}
}
@override
Future<UserCredentialPlatform> getRedirectResult() async {
try {
return UserCredentialWeb(
this,
await delegate.getRedirectResult(),
_webAuth,
);
} catch (e) {
throw getFirebaseAuthException(e);
}
}
@override
Stream<UserPlatform?> authStateChanges() async* {
await _initialized.future;
yield currentUser;
yield* _authStateChangesListeners[app.name]!.stream;
}
@override
Stream<UserPlatform?> idTokenChanges() async* {
await _initialized.future;
yield currentUser;
yield* _idTokenChangesListeners[app.name]!.stream;
}
@override
Stream<UserPlatform?> userChanges() async* {
await _initialized.future;
yield currentUser;
yield* _userChangesListeners[app.name]!.stream;
}
@override
Future<void> sendPasswordResetEmail(
String email, [
ActionCodeSettings? actionCodeSettings,
]) async {
try {
await delegate.sendPasswordResetEmail(
email, convertPlatformActionCodeSettings(actionCodeSettings));
} catch (e) {
throw getFirebaseAuthException(e);
}
}
@override
Future<void> sendSignInLinkToEmail(
String email, [
ActionCodeSettings? actionCodeSettings,
]) async {
try {
await delegate.sendSignInLinkToEmail(
email, convertPlatformActionCodeSettings(actionCodeSettings));
} catch (e) {
throw getFirebaseAuthException(e);
}
}
@override
String get languageCode {
return delegate.languageCode;
}
@override
Future<void> setLanguageCode(String? languageCode) async {
delegate.languageCode = languageCode;
}
@override
Future<void> setSettings({
bool? appVerificationDisabledForTesting,
String? userAccessGroup,
String? phoneNumber,
String? smsCode,
bool? forceRecaptchaFlow,
}) async {
delegate.settings.appVerificationDisabledForTesting =
appVerificationDisabledForTesting;
}
@override
Future<void> setPersistence(Persistence persistence) async {
try {
return delegate.setPersistence(persistence);
} catch (e) {
throw getFirebaseAuthException(e);
}
}
@override
Future<UserCredentialPlatform> signInAnonymously() async {
try {
return UserCredentialWeb(
this,
await delegate.signInAnonymously(),
_webAuth,
);
} catch (e) {
throw getFirebaseAuthException(e, _webAuth);
}
}
@override
Future<UserCredentialPlatform> signInWithCredential(
AuthCredential credential,
) async {
try {
return UserCredentialWeb(
this,
await delegate
.signInWithCredential(convertPlatformCredential(credential)!),
_webAuth,
);
} catch (e) {
throw getFirebaseAuthException(e, _webAuth);
}
}
@override
Future<UserCredentialPlatform> signInWithCustomToken(String token) async {
try {
return UserCredentialWeb(
this,
await delegate.signInWithCustomToken(token),
_webAuth,
);
} catch (e) {
throw getFirebaseAuthException(e, _webAuth);
}
}
@override
Future<UserCredentialPlatform> signInWithEmailAndPassword(
String email, String password) async {
try {
return UserCredentialWeb(
this,
await delegate.signInWithEmailAndPassword(email, password),
_webAuth,
);
} catch (e) {
throw getFirebaseAuthException(e, _webAuth);
}
}
@override
Future<UserCredentialPlatform> signInWithEmailLink(
String email, String emailLink) async {
try {
return UserCredentialWeb(
this,
await delegate.signInWithEmailLink(email, emailLink),
_webAuth,
);
} catch (e) {
throw getFirebaseAuthException(e, _webAuth);
}
}
@override
Future<ConfirmationResultPlatform> signInWithPhoneNumber(
String phoneNumber,
RecaptchaVerifierFactoryPlatform applicationVerifier,
) async {
try {
// Do not inline - type is not inferred & error is thrown.
auth_interop.RecaptchaVerifier verifier = applicationVerifier.delegate;
return ConfirmationResultWeb(
this,
await delegate.signInWithPhoneNumber(phoneNumber, verifier),
_webAuth,
);
} catch (e) {
throw getFirebaseAuthException(e, _webAuth);
}
}
@override
Future<UserCredentialPlatform> signInWithPopup(AuthProvider provider) async {
try {
return UserCredentialWeb(
this,
await delegate.signInWithPopup(convertPlatformAuthProvider(provider)),
_webAuth,
);
} catch (e) {
throw getFirebaseAuthException(e, _webAuth);
}
}
@override
Future<void> signInWithRedirect(AuthProvider provider) async {
try {
return delegate.signInWithRedirect(convertPlatformAuthProvider(provider));
} catch (e) {
throw getFirebaseAuthException(e, _webAuth);
}
}
@override
Future<void> signOut() async {
try {
await delegate.signOut();
} catch (e) {
throw getFirebaseAuthException(e);
}
}
@override
Future<void> useAuthEmulator(String host, int port) async {
try {
// The generic platform interface is with host and port split to
// centralize logic between android/ios native, but web takes the
// origin as a single string
delegate.useAuthEmulator('http://$host:$port');
} catch (e) {
final String code = (e as auth_interop.AuthError).code;
// this catches Firebase Error from web that occurs after hot reloading & hot restarting
if (code != 'auth/emulator-config-failed') {
throw getFirebaseAuthException(e);
}
}
}
@override
Future<String> verifyPasswordResetCode(String code) async {
try {
return await delegate.verifyPasswordResetCode(code);
} catch (e) {
throw getFirebaseAuthException(e);
}
}
@override
Future<void> verifyPhoneNumber({
String? phoneNumber,
PhoneMultiFactorInfo? multiFactorInfo,
required PhoneVerificationCompleted verificationCompleted,
required PhoneVerificationFailed verificationFailed,
required PhoneCodeSent codeSent,
required PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout,
String? autoRetrievedSmsCodeForTesting,
Duration timeout = const Duration(seconds: 30),
int? forceResendingToken,
MultiFactorSession? multiFactorSession,
}) async {
try {
Map<String, dynamic>? data;
if (multiFactorSession != null) {
final _webMultiFactorSession =
multiFactorSession as MultiFactorSessionWeb;
if (multiFactorInfo != null) {
data = {
'multiFactorUid': multiFactorInfo.uid,
'session': _webMultiFactorSession.webSession.jsObject,
};
} else {
data = {
'phoneNumber': phoneNumber,
'session': _webMultiFactorSession.webSession.jsObject,
};
}
}
final phoneOptions = (data ?? phoneNumber)!;
final provider = auth_interop.PhoneAuthProvider(_webAuth);
final verifier = RecaptchaVerifierFactoryWeb(
auth: this,
).delegate;
/// We add the passthrough method for LegacyJsObject
final verificationId = await provider.verifyPhoneNumber(
jsify(phoneOptions, (object) => object), verifier);
codeSent(verificationId, null);
} catch (e) {
verificationFailed(getFirebaseAuthException(e));
}
}
}