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

[firebase_auth] Unhandled Exception: PlatformException(ERROR_INVALID_VERIFICATION_CODE) #3141

Closed
definitelyme opened this issue Aug 11, 2020 · 4 comments

Comments

@definitelyme
Copy link

definitelyme commented Aug 11, 2020

Describe the bug

E/flutter (10238): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: PlatformException(ERROR_INVALID_VERIFICATION_CODE, The sms verification code used to create the phone auth credential is invalid. Please resend the verification code sms and be sure use the verification code provided by the user., null)

I cannot handle the above exception using try / catch when an invalid SMS verification code is passed to the firebaseUser.updatePhoneNumberCredential(credential) method

To Reproduce
Steps to reproduce the behavior:

  1. Authenticate user using FirebaseAuth
FirebaseAuth.instance.verifyPhoneNumber(phoneNumber: "+XXX XXX XX XXXX");
  1. Get an instance of the Authenticated User
var firebaseUser = await FirebaseAuth.instance.currentUser();
  1. Get Phone credentials & update phone number
try {
    final credential = PhoneAuthProvider.getCredential(verificationId: '-------', smsCode: 'WRONG CODE');
    firebaseUser.updatePhoneNumberCredential(credential);
} catch (e) {
    print("Exception caught => $e");
}

Expected behavior
The try / catch block should handle the exception rather than rethrowing / ignoring it.
In my case, a debug print in the console with the exception ->

Exception caught => [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: PlatformException(ERROR_INVALID_VERIFICATION_CODE, The sms verification code used to create the phone auth credential is invalid. Please resend the verification code sms and be sure use the verification code provided by the user., null)
E/flutter (10238): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:572:7)
E/flutter (10238): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:161:18)
E/flutter (10238): <asynchronous suspension>
E/flutter (10238): #2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:334:12)
E/flutter (10238): #3      MethodChannelFirebaseAuth.updatePhoneNumberCredential (package:firebase_auth_platform_interface/src/method_channel_firebase_auth.dart:257:20)
E/flutter (10238): #4      FirebaseUser.updatePhoneNumberCredential (package:firebase_auth/src/firebase_user.dart:135:10)
E/flutter (10238): #5      FirebaseAuthImpl.withPhoneCredential (package:eazox/features/auth/presentation/manager/repo/firebase_auth_impl.dart:137:43)
E/flutter (10238): <asynchronous suspension>
E/flutter (10238): #6      AuthBloc._mapVerifyPhoneWithCredential (package:eazox/features/auth/presentation/manager/logic/auth_bloc.dart:236:35)
E/flutter (10238): <asynchronous suspension> ...

...

Flutter doctor
Run flutter doctor and paste the output below:

[√] Flutter (Channel beta, 1.20.0-7.2.pre, on Microsoft Windows [Version 10.0.16299.15], locale en-US)
    • Flutter version 1.20.0-7.2.pre at C:\src\flutter
    • Framework revision a2bde82fbd (3 weeks ago), 2020-07-18 15:16:35 -0700
    • Engine revision 60b269d898
    • Dart version 2.9.0 (build 2.9.0-21.2.beta)


[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at \AppData\Local\Android\Sdk
    • Platform android-29, build-tools 29.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 3.6)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 46.0.1
    • Dart plugin version 192.8052
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)

[√] VS Code, 64-bit edition (version 1.45.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.11.0

[√] Connected device (3 available)
    • F103 Pro (mobile) • 192.168.43.14:5556 • android-arm64  • Android 6.0 (API 23)
    • Web Server (web)  • web-server         • web-javascript • Flutter Tools
    • Chrome (web)      • chrome             • web-javascript • Google Chrome 84.0.4147.105

• No issues found!
@definitelyme definitelyme changed the title [firebase_auth] ERROR_INVALID_VERIFICATION_CODE [firebase_auth] Unhandled Exception: PlatformException(ERROR_INVALID_VERIFICATION_CODE) Aug 11, 2020
@Infucio
Copy link

Infucio commented Aug 11, 2020

Can you try the below syntax
try { // your firebase calls } on PlatformException catch (PlatformException e) { // do custom magic by acessing e.code and e.message for more information on it }

@definitelyme
Copy link
Author

definitelyme commented Aug 12, 2020

Can you try the below syntax
try { // your firebase calls } on PlatformException catch (PlatformException e) { // do custom magic by acessing e.code and e.message for more information on it }

(e) is already an instance of the Exception PlatformException.

'catch' must be followed by '(identifier)' or '(identifier, identifier)'.
No types are needed, the first is given by 'on', the second is always 'StackTrace'.

Here's what I did

void signInWithPhone(String phone) {
  // your firebase calls
  // Authenticate user with Firebase
  FirebaseAuth.instance.verifyPhoneNumber(
    phoneNumber: "+XXX XXX XX XXXX",
    timeout: Duration(seconds: 60),
    codeSent: (verificationId, [int token]) {
      // Perform action
      // Navigate to another screen & input CODE
    },
    codeAutoRetrievalTimeout: (verificationId) {},
    verificationCompleted: (credentials) => null,
    verificationFailed: (ex) => print("$ex"),
  );
  ...
}

void verifyCode(String verificationId, String code) async {
  try {
    var firebaseUser = await FirebaseAuth.instance.currentUser();

    final credential = PhoneAuthProvider.getCredential(verificationId: verificationId, smsCode: code);

    firebaseUser.updatePhoneNumberCredential(credential);
  } on PlatformException catch (e) {
    print("Exception caught => ${e.code}");
  }
}

// Call function "verifyCode" somewhere with an invalid verification code
verifyCode("---------", "WRONG CODE")

And it still throws the exception PlatformException(ERROR_INVALID_VERIFICATION_CODE) when an invalid verification code is used.

@Salakar
Copy link
Member

Salakar commented Aug 25, 2020

Hey 👋

Our rework of the firebase_auth plugin as part of the FlutterFire roadmap was published over a week ago with a ton of fixes and new features. Please could you try the new version and see if this is still an issue for you? If it is then please submit a new up to date GitHub issue.

See: https://firebase.flutter.dev/docs/auth/error-handling

For help migrating to the new plugins please see the new migration guide: https://firebase.flutter.dev/docs/migration

@Salakar Salakar closed this as completed Aug 25, 2020
@definitelyme
Copy link
Author

Having updated my version of firebase_auth and included the compulsory 'Firebase.initializeApp()' before using FlutterFire features. I still can not catch the exception - ERROR_INVALID_VERIFICATION_CODE.

New
I found a similar bug.
If User A verifies a phone number (+AAA AAA BB BBB) and User B attempts to update their profile using the same phone number above by calling firebaseUser.updatePhoneNumberCredential(credential); the thrown exception "account-exists-with-different-credential" is not caught by the try/catch block.

Example:

try {
    // Authenticate user using existing phone number credential
    final credential = PhoneAuthProvider.getCredential(verificationId: '-------', smsCode: 'VALID CODE');
    firebaseUser.updatePhoneNumberCredential(credential);
} catch (e) {
   // The exception is not caught here
    print("Exception caught => $e");
}

@firebase firebase locked and limited conversation to collaborators Sep 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants