logInWithPhone method

Future<void> logInWithPhone(
  1. String phoneNumber,
  2. void onCodeSent()
)

Starts the Sign In with Phone Flow.

Throws a LogInWithGoogleFailure if an exception occurs.

Implementation

Future<void> logInWithPhone(
  String phoneNumber,
  void Function() onCodeSent,
) async {
  final completer = Completer<void>();
  // try {
  await _firebaseAuth.verifyPhoneNumber(
    phoneNumber: phoneNumber,
    verificationCompleted: (PhoneAuthCredential credential) async {
      try {
        // ANDROID ONLY!

        // Sign the user in (or link) with the auto-generated credential
        await _firebaseAuth.signInWithCredential(credential);

        if (!completer.isCompleted) {
          completer.complete();
        }
      } catch (e) {
        if (!completer.isCompleted) {
          completer.completeError(e);
        }
      }
    },
    verificationFailed: (FirebaseAuthException e) {
      if (!completer.isCompleted) {
        completer.completeError(LogInWithGoogleFailure.fromCode(e.code));
      }
    },
    codeSent: (String verificationId, int? resendToken) {
      this.verificationId = verificationId;
      onCodeSent();
    },
    codeAutoRetrievalTimeout: (String verificationId) {},
  );

  return completer.future;
  // } on FirebaseAuthException catch (e) {
  //   throw LogInWithGoogleFailure.fromCode(e.code);
  // } catch (_) {
  //   throw const LogInWithGoogleFailure();
  // }
}