-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathfirebase_auth_web.dart
641 lines (564 loc) · 18.9 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// 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 'dart:js_interop';
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/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/foundation.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:web/web.dart' as web;
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;
enum StateListener { authStateChange, userStateChange, idTokenChange }
/// 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();
/// The entry point for the [FirebaseAuthWeb] class.
FirebaseAuthWeb({required FirebaseApp app}) : super(appInstance: app) {
// Create a app instance broadcast stream for both delegate listener events
_createStreamListener(app.name, StateListener.authStateChange);
_createStreamListener(app.name, StateListener.idTokenChange);
_createStreamListener(app.name, StateListener.userStateChange);
}
/// Called by PluginRegistry to register this plugin for Flutter Web
static void registerWith(Registrar registrar) {
FirebaseCoreWeb.registerService(
'auth',
ensurePluginInitialized: (firebaseApp) async {
final authDelegate = auth_interop.getAuthInstance(firebaseApp);
// if localhost, and emulator was previously set in localStorage, use it
if (web.window.location.hostname == 'localhost' && kDebugMode) {
final String? emulatorOrigin = web.window.sessionStorage
.getItem(getOriginName(firebaseApp.name));
if (emulatorOrigin != null) {
try {
authDelegate.useAuthEmulator(emulatorOrigin);
// ignore: avoid_print
print(
'Using previously configured Auth emulator at $emulatorOrigin for ${firebaseApp.name} \nTo switch back to production, restart your app with the emulator turned off.',
);
} catch (e) {
if (e.toString().contains('sooner')) {
// Happens during hot reload when the emulator is already configured
// ignore: avoid_print
print(
'Auth emulator is already configured at $emulatorOrigin for ${firebaseApp.name} and kept across hot reload.\nTo switch back to production, restart your app with the emulator turned off.',
);
} else {
rethrow;
}
}
}
}
await authDelegate.onWaitInitState();
},
);
FirebaseAuthPlatform.instance = FirebaseAuthWeb.instance;
PhoneMultiFactorGeneratorPlatform.instance = PhoneMultiFactorGeneratorWeb();
TotpMultiFactorGeneratorPlatform.instance = TotpMultiFactorGeneratorWeb();
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._();
}
bool _cancelUserStream = false;
bool _cancelIdTokenStream = false;
void _createStreamListener(String appName, StateListener stateListener) {
switch (stateListener) {
case StateListener.authStateChange:
_authStateChangesListeners[appName] =
StreamController<UserPlatform?>.broadcast(
onCancel: () {
_authStateChangesListeners[appName]!.close();
_authStateChangesListeners.remove(appName);
delegate.authStateController?.close();
},
);
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);
});
break;
case StateListener.idTokenChange:
_cancelIdTokenStream = false;
_idTokenChangesListeners[appName] =
StreamController<UserPlatform?>.broadcast(
onCancel: () {
if (_userChangesListeners[appName] == null) {
// We cannot remove if there is a userChanges listener as we use this stream for it
_idTokenChangesListeners[appName]!.close();
_idTokenChangesListeners.remove(appName);
delegate.idTokenController?.close();
} else {
// We need to do this because if idTokenListener and userChanges are being listened to
// We need to cancel both at the same time otherwise neither will be closed & removed
_cancelIdTokenStream = true;
}
if (_cancelUserStream) {
_userChangesListeners[appName]!.close();
_userChangesListeners.remove(appName);
}
},
);
// 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);
});
break;
case StateListener.userStateChange:
_cancelUserStream = false;
_userChangesListeners[appName] =
StreamController<UserPlatform?>.broadcast(
onCancel: () {
if (_idTokenChangesListeners[appName] == null) {
_userChangesListeners[appName]!.close();
_userChangesListeners.remove(appName);
// There is no delegate for userChanges as we use idTokenChanges
} else {
_cancelUserStream = true;
}
if (_cancelIdTokenStream) {
// We need to do this because if idTokenListener and userChanges are being listened to
// We need to cancel both at the same time otherwise neither will be closed & removed
_idTokenChangesListeners[appName]!.close();
_idTokenChangesListeners.remove(appName);
delegate.idTokenController?.close();
}
},
);
break;
}
}
/// 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));
return _webAuth!;
}
@override
FirebaseAuthPlatform delegateFor({required FirebaseApp app}) {
return FirebaseAuthWeb(app: app);
}
@override
FirebaseAuthWeb setInitialValues({
PigeonUserDetails? 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 {
await guardAuthExceptions(
() => delegate.applyActionCode(code),
);
}
@override
Future<ActionCodeInfo> checkActionCode(String code) async {
final actionCode = await guardAuthExceptions(
() => delegate.checkActionCode(code),
);
return convertWebActionCodeInfo(actionCode)!;
}
@override
Future<void> confirmPasswordReset(String code, String newPassword) async {
return guardAuthExceptions(
() => delegate.confirmPasswordReset(code, newPassword),
);
}
@override
Future<UserCredentialPlatform> createUserWithEmailAndPassword(
String email, String password) async {
final userCredential = await guardAuthExceptions(
() => delegate.createUserWithEmailAndPassword(email, password),
);
return UserCredentialWeb(
this,
userCredential,
_webAuth,
);
}
@override
Future<List<String>> fetchSignInMethodsForEmail(String email) async {
return guardAuthExceptions(
() => delegate.fetchSignInMethodsForEmail(email),
);
}
@override
Future<UserCredentialPlatform> getRedirectResult() async {
final userCredential =
await guardAuthExceptions(delegate.getRedirectResult);
return UserCredentialWeb(
this,
userCredential,
_webAuth,
);
}
@override
Stream<UserPlatform?> authStateChanges() async* {
await _initialized.future;
yield currentUser;
if (_authStateChangesListeners[app.name] == null) {
_createStreamListener(app.name, StateListener.authStateChange);
}
yield* _authStateChangesListeners[app.name]!.stream;
}
@override
Stream<UserPlatform?> idTokenChanges() async* {
await _initialized.future;
yield currentUser;
if (_idTokenChangesListeners[app.name] == null) {
_createStreamListener(app.name, StateListener.idTokenChange);
}
yield* _idTokenChangesListeners[app.name]!.stream;
}
@override
Stream<UserPlatform?> userChanges() async* {
await _initialized.future;
yield currentUser;
if (_userChangesListeners[app.name] == null) {
_createStreamListener(app.name, StateListener.userStateChange);
}
yield* _userChangesListeners[app.name]!.stream;
}
@override
Future<void> sendPasswordResetEmail(
String email, [
ActionCodeSettings? actionCodeSettings,
]) async {
return guardAuthExceptions(
() => delegate.sendPasswordResetEmail(
email,
convertPlatformActionCodeSettings(
actionCodeSettings,
),
),
);
}
@override
Future<void> sendSignInLinkToEmail(
String email, [
ActionCodeSettings? actionCodeSettings,
]) async {
return guardAuthExceptions(
() => delegate.sendSignInLinkToEmail(
email,
convertPlatformActionCodeSettings(
actionCodeSettings,
),
),
);
}
@override
String get languageCode {
return delegate.languageCode ?? 'en';
}
@override
Future<void> setLanguageCode(String? languageCode) async {
if (languageCode == null) {
delegate.useDeviceLanguage();
} else {
delegate.languageCode = languageCode;
}
}
@override
Future<void> setSettings({
bool? appVerificationDisabledForTesting,
String? userAccessGroup,
String? phoneNumber,
String? smsCode,
bool? forceRecaptchaFlow,
}) async {
delegate.settings.appVerificationDisabledForTesting =
appVerificationDisabledForTesting?.toJS;
}
@override
Future<void> setPersistence(Persistence persistence) async {
return guardAuthExceptions(
() => delegate.setPersistence(
persistence,
),
);
}
@override
Future<UserCredentialPlatform> signInAnonymously() async {
final userCredential = await guardAuthExceptions(
delegate.signInAnonymously,
auth: _webAuth,
);
return UserCredentialWeb(
this,
userCredential,
_webAuth,
);
}
@override
Future<UserCredentialPlatform> signInWithCredential(
AuthCredential credential,
) async {
final authCredential = await guardAuthExceptions(
() =>
delegate.signInWithCredential(convertPlatformCredential(credential)!),
auth: _webAuth,
);
return UserCredentialWeb(
this,
authCredential,
_webAuth,
);
}
@override
Future<UserCredentialPlatform> signInWithCustomToken(String token) async {
final userCredential = await guardAuthExceptions(
() => delegate.signInWithCustomToken(token),
auth: _webAuth,
);
return UserCredentialWeb(
this,
userCredential,
_webAuth,
);
}
@override
Future<UserCredentialPlatform> signInWithEmailAndPassword(
String email, String password) async {
final userCredential = await guardAuthExceptions(
() => delegate.signInWithEmailAndPassword(email, password),
auth: _webAuth,
);
return UserCredentialWeb(
this,
userCredential,
_webAuth,
);
}
@override
Future<UserCredentialPlatform> signInWithEmailLink(
String email, String emailLink) async {
final userCredential = await guardAuthExceptions(
() => delegate.signInWithEmailLink(email, emailLink),
auth: _webAuth,
);
return UserCredentialWeb(
this,
userCredential,
_webAuth,
);
}
@override
Future<ConfirmationResultPlatform> signInWithPhoneNumber(
String phoneNumber,
RecaptchaVerifierFactoryPlatform applicationVerifier,
) async {
// Do not inline - type is not inferred & error is thrown.
auth_interop.RecaptchaVerifier verifier = applicationVerifier.delegate;
final confirmationResult = await guardAuthExceptions(
() => delegate.signInWithPhoneNumber(
phoneNumber,
verifier,
),
);
return ConfirmationResultWeb(
this,
confirmationResult,
_webAuth,
);
}
@override
Future<UserCredentialPlatform> signInWithPopup(AuthProvider provider) async {
final userCredential = await guardAuthExceptions(
() => delegate.signInWithPopup(
convertPlatformAuthProvider(provider),
),
auth: _webAuth,
);
return UserCredentialWeb(
this,
userCredential,
_webAuth,
);
}
@override
Future<void> signInWithRedirect(AuthProvider provider) async {
return guardAuthExceptions(
() => delegate.signInWithRedirect(
convertPlatformAuthProvider(provider),
),
auth: _webAuth,
);
}
@override
Future<void> signOut() async {
return guardAuthExceptions(delegate.signOut);
}
@override
Future<void> useAuthEmulator(String host, int port) async {
try {
// Get current session storage value
final String? emulatorOrigin =
web.window.sessionStorage.getItem(getOriginName(delegate.app.name));
// 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
final String origin = 'http://$host:$port';
if (origin == emulatorOrigin) {
// If the origin is the same as the current one, do nothing
// The emulator was already started at the app start
return;
}
delegate.useAuthEmulator(origin);
// Save to session storage so that the emulator is used on refresh
// only in debug mode
if (kDebugMode) {
web.window.sessionStorage
.setItem(getOriginName(delegate.app.name), origin);
}
} catch (e) {
// Cannot be done with 3.2 constraints
// ignore: invalid_runtime_check_with_js_interop_types
if (e is auth_interop.AuthError) {
final String code = e.code.toDart;
// this catches Firebase Error from web that occurs after hot reloading & hot restarting
if (code != 'auth/emulator-config-failed') {
throw getFirebaseAuthException(e);
}
} else {
rethrow;
}
}
}
@override
Future<String> verifyPasswordResetCode(String code) async {
return guardAuthExceptions(
() => delegate.verifyPasswordResetCode(code),
);
}
@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(phoneOptions.jsify(), verifier);
codeSent(verificationId, null);
} catch (e) {
verificationFailed(getFirebaseAuthException(e));
}
}
@override
Future<void> revokeTokenWithAuthorizationCode(
String authorizationCode) async {
throw UnimplementedError(
'revokeTokenWithAuthorizationCode() is only available on apple platforms.',
);
}
}
String getOriginName(String appName) {
return '$appName-firebaseEmulatorOrigin';
}