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

feat(auth): improve error handling when Email enumeration feature is on #10591

Merged
merged 3 commits into from
Mar 16, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/firebase_auth/firebase_auth/example/lib/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,12 @@ class _AuthGateState extends State<AuthGate> {
setState(() {
error = '$e';
});
} finally {
setIsLoading();
}
setIsLoading();
}

Future<void> _emailAndPassword() async {
if (formKey.currentState?.validate() ?? false) {
setIsLoading();
if (mode == AuthMode.login) {
await auth.signInWithEmailAndPassword(
email: emailController.text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ - (void)signInWithCredential:(id)arguments
} else {
if (error.code == FIRAuthErrorCodeSecondFactorRequired) {
[self handleMultiFactorError:arguments withResult:result withError:error];
} else if (error.code == FIRAuthErrorCodeInternalError) {
[self handleInternalError:arguments withResult:result withError:error];
} else {
result.error(nil, nil, nil, error);
}
Expand Down Expand Up @@ -734,6 +736,8 @@ - (void)signInWithCustomToken:(id)arguments
if (error != nil) {
if (error.code == FIRAuthErrorCodeSecondFactorRequired) {
[self handleMultiFactorError:arguments withResult:result withError:error];
} else if (error.code == FIRAuthErrorCodeInternalError) {
[self handleInternalError:arguments withResult:result withError:error];
} else {
result.error(nil, nil, nil, error);
}
Expand All @@ -743,6 +747,19 @@ - (void)signInWithCustomToken:(id)arguments
}];
}

- (void)handleInternalError:(id)arguments
withResult:(FLTFirebaseMethodCallResult *)result
withError:(NSError *)error {
const NSError *underlyingError = error.userInfo[@"NSUnderlyingError"];
if (underlyingError != nil) {
const NSDictionary *details =
underlyingError.userInfo[@"FIRAuthErrorUserInfoDeserializedResponseKey"];
result.error(nil, nil, details, underlyingError);
return;
}
result.error(nil, nil, nil, error);
}

- (void)handleMultiFactorError:(id)arguments
withResult:(FLTFirebaseMethodCallResult *)result
withError:(NSError *_Nullable)error {
Expand Down Expand Up @@ -801,6 +818,8 @@ - (void)signInWithEmailAndPassword:(id)arguments
if (error != nil) {
if (error.code == FIRAuthErrorCodeSecondFactorRequired) {
[self handleMultiFactorError:arguments withResult:result withError:error];
} else if (error.code == FIRAuthErrorCodeInternalError) {
[self handleInternalError:arguments withResult:result withError:error];
} else {
result.error(nil, nil, nil, error);
}
Expand All @@ -819,6 +838,8 @@ - (void)signInWithEmailLink:(id)arguments
if (error != nil) {
if (error.code == FIRAuthErrorCodeSecondFactorRequired) {
[self handleMultiFactorError:arguments withResult:result withError:error];
} else if (error.code == FIRAuthErrorCodeInternalError) {
[self handleInternalError:arguments withResult:result withError:error];
} else {
result.error(nil, nil, nil, error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,27 @@ FirebaseException platformExceptionToFirebaseAuthException(

message = details['message'] ?? message;

if (details['additionalData'] != null) {
if (details['additionalData']['authCredential'] != null) {
final additionalData = details['additionalData'];

if (additionalData != null) {
if (additionalData['authCredential'] != null) {
credential = AuthCredential(
providerId: details['additionalData']['authCredential']['providerId'],
signInMethod: details['additionalData']['authCredential']
['signInMethod'],
token: details['additionalData']['authCredential']['token'],
providerId: additionalData['authCredential']['providerId'],
signInMethod: additionalData['authCredential']['signInMethod'],
token: additionalData['authCredential']['token'],
);
}

if (details['additionalData']['email'] != null) {
email = details['additionalData']['email'];
if (additionalData['email'] != null) {
email = additionalData['email'];
}
}
// This code happens when using Enumerate Email protection
// The error code is only returned in a String on Android
if (additionalData?['message'] == 'INVALID_LOGIN_CREDENTIALS' ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have a list of error codes somewhere? would it make sense to have an enum with all errors?
something like

enum FirebaseAuthErrorCode {
  invalidLoginCredentials('INVALID_LOGIN_CREDENTIALS');

  final String code;
  const FirebaseAuthErrorCode(this.code);
}

potentially out of scope of this PR, but could be useful

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't find it in the Firebase Android or iOS repository 🤔 I will ask next call with Firebase 👍

(message?.contains('INVALID_LOGIN_CREDENTIALS') ?? false)) {
code = 'INVALID_LOGIN_CREDENTIALS';
}
}
return FirebaseAuthException(
code: code,
Expand Down