Skip to content

Commit

Permalink
fix(auth): properly cast the PlatformException to FirebaseAuthExcepti…
Browse files Browse the repository at this point in the history
…on (#10058)
  • Loading branch information
Lyokone committed Dec 8, 2022
1 parent 6631da6 commit 6c8f951
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 40 deletions.
Expand Up @@ -5,6 +5,7 @@
import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart';
import 'package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart';
import 'package:firebase_auth_platform_interface/src/method_channel/method_channel_user_credential.dart';
import 'package:firebase_auth_platform_interface/src/method_channel/utils/exception.dart';
import 'package:firebase_auth_platform_interface/src/method_channel/utils/pigeon_helper.dart';
import 'package:firebase_auth_platform_interface/src/pigeon/messages.pigeon.dart';

Expand All @@ -16,8 +17,12 @@ class MethodChannelMultiFactor extends MultiFactorPlatform {

@override
Future<MultiFactorSession> getSession() async {
final pigeonObject = await _api.getSession(auth.app.name);
return MultiFactorSession(pigeonObject.id);
try {
final pigeonObject = await _api.getSession(auth.app.name);
return MultiFactorSession(pigeonObject.id);
} catch (e, stack) {
convertPlatformException(e, stack, fromPigeon: true);
}
}

@override
Expand All @@ -39,14 +44,18 @@ class MethodChannelMultiFactor extends MultiFactorPlatform {
throw ArgumentError('verificationId must not be null');
}

await _api.enrollPhone(
auth.app.name,
PigeonPhoneMultiFactorAssertion(
verificationId: verificationId,
verificationCode: verificationCode,
),
displayName,
);
try {
await _api.enrollPhone(
auth.app.name,
PigeonPhoneMultiFactorAssertion(
verificationId: verificationId,
verificationCode: verificationCode,
),
displayName,
);
} catch (e, stack) {
convertPlatformException(e, stack, fromPigeon: true);
}
} else {
throw UnimplementedError(
'Credential type ${_assertion.credential} is not supported yet',
Expand All @@ -66,16 +75,24 @@ class MethodChannelMultiFactor extends MultiFactorPlatform {
);
}

return _api.unenroll(
auth.app.name,
uidToUnenroll,
);
try {
return _api.unenroll(
auth.app.name,
uidToUnenroll,
);
} catch (e, stack) {
convertPlatformException(e, stack, fromPigeon: true);
}
}

@override
Future<List<MultiFactorInfo>> getEnrolledFactors() async {
final data = await _api.getEnrolledFactors(auth.app.name);
return multiFactorInfoPigeonToObject(data);
try {
final data = await _api.getEnrolledFactors(auth.app.name);
return multiFactorInfoPigeonToObject(data);
} catch (e, stack) {
convertPlatformException(e, stack, fromPigeon: true);
}
}
}

Expand Down Expand Up @@ -112,18 +129,22 @@ class MethodChannelMultiFactorResolver extends MultiFactorResolverPlatform {
throw ArgumentError('verificationId must not be null');
}

final data = await _api.resolveSignIn(
_resolverId,
PigeonPhoneMultiFactorAssertion(
verificationId: verificationId,
verificationCode: verificationCode,
),
);

MethodChannelUserCredential userCredential =
MethodChannelUserCredential(_auth, data.cast<String, dynamic>());

return userCredential;
try {
final data = await _api.resolveSignIn(
_resolverId,
PigeonPhoneMultiFactorAssertion(
verificationId: verificationId,
verificationCode: verificationCode,
),
);

MethodChannelUserCredential userCredential =
MethodChannelUserCredential(_auth, data.cast<String, dynamic>());

return userCredential;
} catch (e, stack) {
convertPlatformException(e, stack, fromPigeon: true);
}
} else {
throw UnimplementedError(
'Credential type ${_assertion.credential} is not supported yet',
Expand Down
Expand Up @@ -14,13 +14,17 @@ import 'package:flutter/services.dart';

/// Catches a [PlatformException] and converts it into a [FirebaseAuthException]
/// if it was intentionally caught on the native platform.
Never convertPlatformException(Object exception, StackTrace stackTrace) {
Never convertPlatformException(
Object exception,
StackTrace stackTrace, {
bool fromPigeon = false,
}) {
if (exception is! PlatformException) {
Error.throwWithStackTrace(exception, stackTrace);
}

Error.throwWithStackTrace(
platformExceptionToFirebaseAuthException(exception),
platformExceptionToFirebaseAuthException(exception, fromPigeon: fromPigeon),
stackTrace,
);
}
Expand All @@ -32,8 +36,17 @@ Never convertPlatformException(Object exception, StackTrace stackTrace) {
/// messages which can be converted into user friendly exceptions.
// TODO(rousselGit): Should this return a FirebaseAuthException to avoid having to cast?
FirebaseException platformExceptionToFirebaseAuthException(
PlatformException platformException,
) {
PlatformException platformException, {
bool fromPigeon = false,
}) {
if (fromPigeon) {
return FirebaseAuthException(
code: platformException.code,
// Remove leading classname from message
message: platformException.message?.split(': ').last,
);
}

Map<String, dynamic>? details = platformException.details != null
? Map<String, dynamic>.from(platformException.details)
: null;
Expand Down
Expand Up @@ -58,9 +58,13 @@ class MultiFactorWeb extends MultiFactorPlatform {
);
}

return _webMultiFactorUser.unenroll(
uidToUnenroll,
);
try {
return _webMultiFactorUser.unenroll(
uidToUnenroll,
);
} catch (e) {
throw getFirebaseAuthException(e);
}
}

@override
Expand Down Expand Up @@ -106,11 +110,15 @@ class MultiFactorResolverWeb extends MultiFactorResolverPlatform {
) async {
final webAssertion = assertion as MultiFactorAssertionWeb;

return UserCredentialWeb(
_auth,
await _webMultiFactorResolver.resolveSignIn(webAssertion.assertion),
_webAuth,
);
try {
return UserCredentialWeb(
_auth,
await _webMultiFactorResolver.resolveSignIn(webAssertion.assertion),
_webAuth,
);
} catch (e) {
throw getFirebaseAuthException(e);
}
}
}

Expand Down

0 comments on commit 6c8f951

Please sign in to comment.