Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth): properly cast the PlatformException to FirebaseAuthException #10058

Merged
merged 1 commit into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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