Skip to content

Commit 9ad97c8

Browse files
authored
fix(auth): properly propagate the FirebaseAuthMultiFactorException for all reauthenticate and link methods (#9700)
* fix(auth): properly propagate the FirebaseAuthMultiFactorException for all reauthenticate and link methods * fix(auth, ios): properly propagate the FirebaseAuthMultiFactorException for all reauthenticate and link methods * fix(auth): properly propagate the FirebaseAuthMultiFactorException for all reauthenticate and link methods * fix(auth): properly propagate the FirebaseAuthMultiFactorException for all reauthenticate and link methods
1 parent 2a1f910 commit 9ad97c8

File tree

9 files changed

+177
-68
lines changed

9 files changed

+177
-68
lines changed

packages/firebase_auth/firebase_auth/android/src/main/java/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,10 @@ private Task<Map<String, Object>> linkUserWithCredential(Map<String, Object> arg
11281128

11291129
taskCompletionSource.setResult(parseAuthResult(authResult));
11301130
} catch (Exception e) {
1131+
if (e.getCause() instanceof FirebaseAuthMultiFactorException) {
1132+
handleMultiFactorException(arguments, taskCompletionSource, e);
1133+
return;
1134+
}
11311135
String message = e.getMessage();
11321136

11331137
if (message != null
@@ -1169,7 +1173,11 @@ private Task<Map<String, Object>> reauthenticateUserWithCredential(
11691173
Tasks.await(firebaseUser.reauthenticateAndRetrieveData(credential));
11701174
taskCompletionSource.setResult(parseAuthResult(authResult));
11711175
} catch (Exception e) {
1172-
taskCompletionSource.setException(e);
1176+
if (e.getCause() instanceof FirebaseAuthMultiFactorException) {
1177+
handleMultiFactorException(arguments, taskCompletionSource, e);
1178+
} else {
1179+
taskCompletionSource.setException(e);
1180+
}
11731181
}
11741182
});
11751183

packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,13 @@ - (void)userLinkWithCredential:(id)arguments
10641064
[currentUser linkWithCredential:credential
10651065
completion:^(FIRAuthDataResult *authResult, NSError *error) {
10661066
if (error != nil) {
1067-
result.error(nil, nil, nil, error);
1067+
if (error.code == FIRAuthErrorCodeSecondFactorRequired) {
1068+
[self handleMultiFactorError:arguments
1069+
withResult:result
1070+
withError:error];
1071+
} else {
1072+
result.error(nil, nil, nil, error);
1073+
}
10681074
} else {
10691075
result.success(authResult);
10701076
}
@@ -1090,7 +1096,13 @@ - (void)userReauthenticateUserWithCredential:(id)arguments
10901096
[currentUser reauthenticateWithCredential:credential
10911097
completion:^(FIRAuthDataResult *authResult, NSError *error) {
10921098
if (error != nil) {
1093-
result.error(nil, nil, nil, error);
1099+
if (error.code == FIRAuthErrorCodeSecondFactorRequired) {
1100+
[self handleMultiFactorError:arguments
1101+
withResult:result
1102+
withError:error];
1103+
} else {
1104+
result.error(nil, nil, nil, error);
1105+
}
10941106
} else {
10951107
result.success(authResult);
10961108
}

packages/firebase_auth/firebase_auth/lib/src/user.dart

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,16 @@ class User {
184184
/// - Thrown if the credential is a [PhoneAuthProvider.credential] and the
185185
/// verification ID of the credential is not valid.
186186
Future<UserCredential> linkWithCredential(AuthCredential credential) async {
187-
return UserCredential._(
188-
_auth,
189-
await _delegate.linkWithCredential(credential),
190-
);
187+
try {
188+
return UserCredential._(
189+
_auth,
190+
await _delegate.linkWithCredential(credential),
191+
);
192+
} on FirebaseAuthMultiFactorExceptionPlatform catch (e) {
193+
throw FirebaseAuthMultiFactorException._(_auth, e);
194+
} catch (e) {
195+
rethrow;
196+
}
191197
}
192198

193199
/// Links with an AuthProvider using native authentication flow.
@@ -273,10 +279,16 @@ class User {
273279
Future<UserCredential> reauthenticateWithProvider(
274280
AuthProvider provider,
275281
) async {
276-
return UserCredential._(
277-
_auth,
278-
await _delegate.reauthenticateWithProvider(provider),
279-
);
282+
try {
283+
return UserCredential._(
284+
_auth,
285+
await _delegate.reauthenticateWithProvider(provider),
286+
);
287+
} on FirebaseAuthMultiFactorExceptionPlatform catch (e) {
288+
throw FirebaseAuthMultiFactorException._(_auth, e);
289+
} catch (e) {
290+
rethrow;
291+
}
280292
}
281293

282294
/// Re-authenticates a user using a popup on Web.
@@ -480,12 +492,18 @@ class User {
480492
// also clear that instance before proceeding.
481493
bool mustClear = verifier == null;
482494
verifier ??= RecaptchaVerifier(auth: _delegate.auth);
483-
final result =
484-
await _delegate.linkWithPhoneNumber(phoneNumber, verifier.delegate);
485-
if (mustClear) {
486-
verifier.clear();
495+
try {
496+
final result =
497+
await _delegate.linkWithPhoneNumber(phoneNumber, verifier.delegate);
498+
if (mustClear) {
499+
verifier.clear();
500+
}
501+
return ConfirmationResult._(_auth, result);
502+
} on FirebaseAuthMultiFactorExceptionPlatform catch (e) {
503+
throw FirebaseAuthMultiFactorException._(_auth, e);
504+
} catch (e) {
505+
rethrow;
487506
}
488-
return ConfirmationResult._(_auth, result);
489507
}
490508

491509
/// Re-authenticates a user using a fresh credential.
@@ -520,10 +538,16 @@ class User {
520538
Future<UserCredential> reauthenticateWithCredential(
521539
AuthCredential credential,
522540
) async {
523-
return UserCredential._(
524-
_auth,
525-
await _delegate.reauthenticateWithCredential(credential),
526-
);
541+
try {
542+
return UserCredential._(
543+
_auth,
544+
await _delegate.reauthenticateWithCredential(credential),
545+
);
546+
} on FirebaseAuthMultiFactorExceptionPlatform catch (e) {
547+
throw FirebaseAuthMultiFactorException._(_auth, e);
548+
} catch (e) {
549+
rethrow;
550+
}
527551
}
528552

529553
/// Refreshes the current user, if signed in.

packages/firebase_auth/firebase_auth_web/lib/firebase_auth_web.dart

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
5757
if (webUser == null) {
5858
return null;
5959
} else {
60-
return UserWeb(this,
61-
MultiFactorWeb(this, multi_factor.multiFactor(webUser)), webUser);
60+
return UserWeb(
61+
this,
62+
MultiFactorWeb(this, multi_factor.multiFactor(webUser)),
63+
webUser,
64+
_webAuth,
65+
);
6266
}
6367
}).listen((UserWeb? webUser) {
6468
_authStateChangesListeners[app.name]!.add(webUser);
@@ -70,8 +74,12 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
7074
if (webUser == null) {
7175
return null;
7276
} else {
73-
return UserWeb(this,
74-
MultiFactorWeb(this, multi_factor.multiFactor(webUser)), webUser);
77+
return UserWeb(
78+
this,
79+
MultiFactorWeb(this, multi_factor.multiFactor(webUser)),
80+
webUser,
81+
_webAuth,
82+
);
7583
}
7684
}).listen((UserWeb? webUser) {
7785
_idTokenChangesListeners[app.name]!.add(webUser);
@@ -136,9 +144,11 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
136144
}
137145

138146
return UserWeb(
139-
this,
140-
MultiFactorWeb(this, multi_factor.multiFactor(delegate.currentUser!)),
141-
delegate.currentUser!);
147+
this,
148+
MultiFactorWeb(this, multi_factor.multiFactor(delegate.currentUser!)),
149+
delegate.currentUser!,
150+
_webAuth,
151+
);
142152
}
143153

144154
@override
@@ -192,6 +202,7 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
192202
return UserCredentialWeb(
193203
this,
194204
await delegate.createUserWithEmailAndPassword(email, password),
205+
_webAuth,
195206
);
196207
} catch (e) {
197208
throw getFirebaseAuthException(e);
@@ -210,7 +221,11 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
210221
@override
211222
Future<UserCredentialPlatform> getRedirectResult() async {
212223
try {
213-
return UserCredentialWeb(this, await delegate.getRedirectResult());
224+
return UserCredentialWeb(
225+
this,
226+
await delegate.getRedirectResult(),
227+
_webAuth,
228+
);
214229
} catch (e) {
215230
throw getFirebaseAuthException(e);
216231
}
@@ -297,20 +312,27 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
297312
@override
298313
Future<UserCredentialPlatform> signInAnonymously() async {
299314
try {
300-
return UserCredentialWeb(this, await delegate.signInAnonymously());
315+
return UserCredentialWeb(
316+
this,
317+
await delegate.signInAnonymously(),
318+
_webAuth,
319+
);
301320
} catch (e) {
302-
throw getFirebaseAuthException(e);
321+
throw getFirebaseAuthException(e, _webAuth);
303322
}
304323
}
305324

306325
@override
307326
Future<UserCredentialPlatform> signInWithCredential(
308-
AuthCredential credential) async {
327+
AuthCredential credential,
328+
) async {
309329
try {
310330
return UserCredentialWeb(
311-
this,
312-
await delegate
313-
.signInWithCredential(convertPlatformCredential(credential)!));
331+
this,
332+
await delegate
333+
.signInWithCredential(convertPlatformCredential(credential)!),
334+
_webAuth,
335+
);
314336
} catch (e) {
315337
throw getFirebaseAuthException(e, _webAuth);
316338
}
@@ -320,7 +342,10 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
320342
Future<UserCredentialPlatform> signInWithCustomToken(String token) async {
321343
try {
322344
return UserCredentialWeb(
323-
this, await delegate.signInWithCustomToken(token));
345+
this,
346+
await delegate.signInWithCustomToken(token),
347+
_webAuth,
348+
);
324349
} catch (e) {
325350
throw getFirebaseAuthException(e, _webAuth);
326351
}
@@ -331,7 +356,10 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
331356
String email, String password) async {
332357
try {
333358
return UserCredentialWeb(
334-
this, await delegate.signInWithEmailAndPassword(email, password));
359+
this,
360+
await delegate.signInWithEmailAndPassword(email, password),
361+
_webAuth,
362+
);
335363
} catch (e) {
336364
throw getFirebaseAuthException(e, _webAuth);
337365
}
@@ -342,7 +370,10 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
342370
String email, String emailLink) async {
343371
try {
344372
return UserCredentialWeb(
345-
this, await delegate.signInWithEmailLink(email, emailLink));
373+
this,
374+
await delegate.signInWithEmailLink(email, emailLink),
375+
_webAuth,
376+
);
346377
} catch (e) {
347378
throw getFirebaseAuthException(e, _webAuth);
348379
}
@@ -358,7 +389,10 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
358389
auth_interop.RecaptchaVerifier verifier = applicationVerifier.delegate;
359390

360391
return ConfirmationResultWeb(
361-
this, await delegate.signInWithPhoneNumber(phoneNumber, verifier));
392+
this,
393+
await delegate.signInWithPhoneNumber(phoneNumber, verifier),
394+
_webAuth,
395+
);
362396
} catch (e) {
363397
throw getFirebaseAuthException(e, _webAuth);
364398
}
@@ -370,6 +404,7 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
370404
return UserCredentialWeb(
371405
this,
372406
await delegate.signInWithPopup(convertPlatformAuthProvider(provider)),
407+
_webAuth,
373408
);
374409
} catch (e) {
375410
throw getFirebaseAuthException(e, _webAuth);

packages/firebase_auth/firebase_auth_web/lib/src/firebase_auth_web_confirmation_result.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,25 @@ import 'utils/web_utils.dart';
1414
/// The web delegate implementation for [ConfirmationResultPlatform].
1515
class ConfirmationResultWeb extends ConfirmationResultPlatform {
1616
/// Creates a new [ConfirmationResultWeb] instance.
17-
ConfirmationResultWeb(this._auth, this._webConfirmationResult)
18-
: super(_webConfirmationResult.verificationId);
17+
ConfirmationResultWeb(
18+
this._auth,
19+
this._webConfirmationResult,
20+
this._webAuth,
21+
) : super(_webConfirmationResult.verificationId);
1922

2023
final FirebaseAuthPlatform _auth;
2124

2225
final auth_interop.ConfirmationResult _webConfirmationResult;
26+
final auth_interop.Auth? _webAuth;
2327

2428
@override
2529
Future<UserCredentialPlatform> confirm(String verificationCode) async {
2630
try {
2731
return UserCredentialWeb(
28-
_auth, await _webConfirmationResult.confirm(verificationCode));
32+
_auth,
33+
await _webConfirmationResult.confirm(verificationCode),
34+
_webAuth,
35+
);
2936
} catch (e) {
3037
throw getFirebaseAuthException(e);
3138
}

packages/firebase_auth/firebase_auth_web/lib/src/firebase_auth_web_multi_factor.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:firebase_auth_web/firebase_auth_web.dart';
1010
import 'package:firebase_auth_web/src/firebase_auth_web_user_credential.dart';
1111

1212
import 'interop/auth.dart' as auth;
13+
import 'interop/auth.dart' as auth_interop;
1314
import 'interop/multi_factor.dart' as multi_factor_interop;
1415
import 'utils/web_utils.dart';
1516

@@ -92,9 +93,11 @@ class MultiFactorResolverWeb extends MultiFactorResolverPlatform {
9293
MultiFactorSession session,
9394
this._auth,
9495
this._webMultiFactorResolver,
96+
this._webAuth,
9597
) : super(hints, session);
9698

9799
final multi_factor_interop.MultiFactorResolver _webMultiFactorResolver;
100+
final auth_interop.Auth? _webAuth;
98101
final FirebaseAuthWeb _auth;
99102

100103
@override
@@ -106,6 +109,7 @@ class MultiFactorResolverWeb extends MultiFactorResolverPlatform {
106109
return UserCredentialWeb(
107110
_auth,
108111
await _webMultiFactorResolver.resolveSignIn(webAssertion.assertion),
112+
_webAuth,
109113
);
110114
}
111115
}

0 commit comments

Comments
 (0)