Search code examples
firebaseflutterfirebase-authenticationflutter-websms-verification

How to enable firebase phone verification in flutter web ? I have tried many ways but it doesn't seems to work, SMS code need to be sent to the user


I want to implement the phone verification by sending OTP using firebase, how to do this? . I have tried by following this thread in github, but no use it's not helping, signInwithphoneNumber only signs in user not verifies the user by sending SMS OTP code, how to solve this?

Github thread link:https://github.com/flutter/flutter/issues/46021

When I have implemented throwed the following error:

Error: UnimplementedError: verifyPhoneNumber() is not supported on the web. Please use 
`signInWithPhoneNumber` instead.

Can someone help me out please !!!


Solution

  • You have to use

    FirebaseAuth auth = FirebaseAuth.instance;
    
    // Wait for the user to complete the reCAPTCHA & for an SMS code to be sent.
    ConfirmationResult confirmationResult = await auth.signInWithPhoneNumber('+44 7123 123 456');
    

    then this to verify

    UserCredential userCredential = await confirmationResult.confirm('123456');
    

    You can also add RecaptchaVerifier as per your own use like

    ConfirmationResult confirmationResult = await auth.signInWithPhoneNumber('+44 7123 123 456', RecaptchaVerifier(
      container: 'recaptcha',
      size: RecaptchaVerifierSize.compact,
      theme: RecaptchaVerifierTheme.dark,
    ));
    

    and you can also edit reCaptcha

    RecaptchaVerifier(
      onSuccess: () => print('reCAPTCHA Completed!'),
      onError: (FirebaseAuthException error) => print(error),
      onExpired: () => print('reCAPTCHA Expired!'),
    );