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

Unhandled Exception occurred if input an invalid OTP verification code #3626

Closed
ShohidulProgrammer opened this issue Sep 23, 2020 · 4 comments

Comments

@ShohidulProgrammer
Copy link

ShohidulProgrammer commented Sep 23, 2020

I want to test if a user inputs the wrong OTP code. but, here occurred an Unhandled Exception.

This is my code:

        // signIn by passing otp code 
      signInWithOTP(smsCode, verId) {
           try {
                   AuthCredential credential =
                       PhoneAuthProvider.credential(verificationId: verId, smsCode: smsCode);
                  if (credential != null) {
                        signIn(credential);
                       print('Successfully signIn With OTP code');
                  } else {
                        print('You input invalid OTP code');
                  }
            } catch (e) {
                   print('signInWithOTP credential error: $e');
           }
     }

      //SignIn
    signIn(AuthCredential credential) {
        try {
             FirebaseAuth.instance.signInWithCredential(credential);
         } catch (e) {
             print('signIn signInWithCredential error: $e');
        }
     }

this is the exception:

[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: [firebase_auth/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.
E/flutter (32758): #0      catchPlatformException (package:firebase_auth_platform_interface/src/method_channel/utils/exception.dart:20:3)
E/flutter (32758): #1      _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter (32758): #2      _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (32758): #3      _FutureListener.handleError (dart:async/future_impl.dart:160:20)
E/flutter (32758): #4      Future._propagateToListeners.handleError (dart:async/future_impl.dart:708:47)
E/flutter (32758): #5      Future._propagateToListeners (dart:async/future_impl.dart:729:24)
E/flutter (32758): #6      Future._completeError (dart:async/future_impl.dart:537:5)
E/flutter (32758): #7      _AsyncAwaitCompleter.completeError (dart:async-patch/async_patch.dart:47:15)
E/flutter (32758): #8      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart)
E/flutter (32758): <asynchronous suspension>
E/flutter (32758): #9      MethodChannelFirebaseAuth.signInWithCredential (package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart:389:47)
E/flutter (32758): #10     FirebaseAuth.signInWithCredential (package:firebase_auth/src/firebase_auth.dart:436:31)
E/flutter (32758): #11     AuthService.signIn (package:chat_demo/src/views/otp/services/auth/auth_service.dart:29:29)
E/flutter (32758): #12     AuthService.signInWithOTP (package:chat_demo/src/views/otp/services/auth/auth_service.dart:40:9)
E/flutter (32758): #13     FirebaseOtpProvider.signInWithOtp (package:chat_demo/src/views/otp/view_models/firebase_otp_provider.dart:43:19)
E/flutter (32758): #14     _OtpPageState.build.<anonymous closure> (package:chat_demo/src/views/otp/view/screens/otp_page.dart:54:41)
E/flutter (32758): #15     _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992:19)
E/flutter (32758): #16     _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1098:38)
E/flutter (32758): #17     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24)
E/flutter (32758): #18     TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11)
E/flutter (32758): #19     BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5)
E/flutter (32758): #20     BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:219:7)
E/flutter (32758): #21     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:477:9)
E/flutter (32758): #22     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:78:12)
E/flutter (32758): #23     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:124:9)
E/flutter (32758): #24     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
E/flutter (32758): #25     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:122:18)
E/flutter (32758): #26     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:108:7)
E/flutter (32758): #27     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:220:19)
E/flutter (32758): #28     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22)
E/flutter (32758): #29     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7)
E/flutter (32758): #30     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7)
E/flutter (32758): #31     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7)
E/flutter (32758): #32     _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter (32758): #33     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (32758): #34     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (32758): #35     _invoke1 (dart:ui/hooks.dart:267:10)
E/flutter (32758): #36     _dispatchPointerDataPacket (dart:ui/hooks.dart:176:5)
@ShohidulProgrammer ShohidulProgrammer added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Sep 23, 2020
@ShohidulProgrammer ShohidulProgrammer changed the title Unhandled Exception occurred if input an invalid verification code Unhandled Exception occurred if input an invalid OTP verification code Sep 23, 2020
@ShohidulProgrammer
Copy link
Author

ShohidulProgrammer commented Sep 23, 2020

Ok, I solved it by changing my code. I check if there credential.token != null

Here is my code

   // signIn by passing otp code
   signInWithOTP(smsCode, verId) {
       AuthCredential credential =
           PhoneAuthProvider.credential(verificationId: verId, smsCode: smsCode);
       if (credential.token != null) {
            signIn(credential);
       } else {
             print('You input invalid OTP code');
    }
 }

@darshankawar
Copy link

@ShohidulProgrammer Given your last message, I am going ahead and closing this as solved. If you disagree, write in comments and I'll reopen.
Thanks.

@darshankawar darshankawar removed Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Sep 23, 2020
@ShohidulProgrammer
Copy link
Author

The problem was not solved. coz, credential.token != null is always true.
so, I never can call the "signIn(credential)" method.
If I input the right OTP code without checking the credential.token != null condition then it can create a user.
but, I also want to check whether the input wrong/ invalid OTP code.
if invalid OTP code inputted then the unhandled exception occurred which I already mentioned above.

@ShohidulProgrammer
Copy link
Author

ShohidulProgrammer commented Sep 24, 2020

I solved the issue by adding the catchError method and checking authResult != null.
Here is my updated code:

    // signIn by passing otp code
    signInWithOTP(smsCode, verId) {
        AuthCredential credential = PhoneAuthProvider.credential(
           verificationId: verId,
           smsCode: smsCode,
        );
         // call signin method
         signIn(credential);
     }

       //SignIn
       signIn(AuthCredential credential) async {
         final UserCredential authResult = await FirebaseAuth.instance
             .signInWithCredential(credential)
            .catchError((onError) {
                showSnackBar(msg: 'Something Wrong');
                print('SignIn Error: ${onError.toString()}\n\n');
            });

           if (authResult != null) {
              showSnackBar(msg: 'SignIn Successfully! ');
           } else {
             showSnackBar(msg: 'SignIn Failed ');
          }
      }

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

No branches or pull requests

2 participants