Lakshya Jain
Lakshya Jain

Reputation: 196

Cloud Firestore Document Check after Phone Authentication

I will try to explain it as clearly as possible

I wanted to add a check when verification is complete. In that the check is supposed to be like: Check if there is a document with document id as the user UID which has authenticated. If it is there then go to the home. If it is not there then create a document using updateData class that I have created already and then go to the home page

Here is my code for phone authentication

  Future phoneAuthentication(
    String fullName,
    String phoneNumber,
    String phoneIsoCode,
    String nonInternationalNumber,
    String profilePicture,
    String verificationCode,
    BuildContext context,
  ) async {
    _auth.verifyPhoneNumber(
      phoneNumber: phoneNumber,
      timeout: Duration(seconds: 0),
      verificationCompleted: (AuthCredential authCredential) async {
        _auth.signInWithCredential(authCredential).then(
          (UserCredential result) async {
            User user = result.user;
            await DatabaseService(uid: user.uid).updateUserData(
              fullName,
              phoneNumber,
              phoneIsoCode,
              nonInternationalNumber,
              profilePicture,
            );
            Navigator.pushAndRemoveUntil(
              context,
              MaterialPageRoute(
                builder: (context) => CustomerDashboard(),
              ),
              (route) => false,
            );
          },
        ).catchError(
          (e) {
            return null;
          },
        );
      },
      verificationFailed: (FirebaseAuthException exception) {
        return "Error";
      },
      codeSent: (String verificationId, [int forceResendingToken]) {
        var _credential = PhoneAuthProvider.credential(
          verificationId: verificationId,
          smsCode: verificationCode,
        );
        _auth.signInWithCredential(_credential).then(
          (UserCredential result) async {
            User user = result.user;
            await DatabaseService(uid: user.uid).updateUserData(
              fullName,
              phoneNumber,
              phoneIsoCode,
              nonInternationalNumber,
              profilePicture,
            );
            Navigator.pushAndRemoveUntil(
              context,
              MaterialPageRoute(
                builder: (context) => CustomerDashboard(),
              ),
              (route) => false,
            );
          },
        ).catchError(
          (e) {},
        );
      },
      codeAutoRetrievalTimeout: (String verificationId) {
        verificationId = verificationId;
      },
    );
  }

Please help me how I am supposed to add this check.

Upvotes: 1

Views: 42

Answers (1)

Apps 247
Apps 247

Reputation: 603

I copied & adapted this code here from this Stack Overflow Question

DocumentSnapshot ds = await YOUR_DB_REFERENCE_IDENTIFIER.collection("YOUR_COLLECTION_NAME").document(user.uid).get();
return ds.exists;

Upvotes: 1

Related Questions