1

I have an android app making use of Firebase AuthUI for signing in users.I'm only using phone authentication. If a new user logs in , I want him to be directed to the Profile creation activity, and if he is a returning user, I want him to be directed to the home activity. However ,when I'm using a new test number I'm getting directed to home instead of profile activity. I've tried with multiple numbers, it's working with some and not working (sign in is successful, however detected as old user) with others. To check for a new/old user, I'm using this code:

if (user.getMetadata().getCreationTimestamp() == user.getMetadata().getLastSignInTimestamp()) {
                    //This is a New User
                    //Move to Profile Creation Activity
                    Toast.makeText(this, "new user", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "onActivityResult: creationTimestamp "+user.getMetadata().getCreationTimestamp());
                    Log.d(TAG, "onActivityResult: lastsignin"+user.getMetadata().getLastSignInTimestamp());
                    Intent loginIntent = new Intent(this, ProfileActivity.class);
                    startActivity(loginIntent);
                    this.finish();
                } else {
                    //This is a returning user
                    Toast.makeText(this, "old", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "onActivityResult: creationTimestamp "+user.getMetadata().getCreationTimestamp());
                    Log.d(TAG, "onActivityResult: lastsignin "+user.getMetadata().getLastSignInTimestamp());
                    Intent intent = new Intent(this, HomeActivity.class);
                    startActivity(intent);
                    this.finish();


                }

I have manually added test numbers to Firebase console . I am also repeatedly deleting accounts registered with a mobile number so that I can reuse my test numbers. I don't know if this is causing the problem. I also tried to logging in the creation and last sign in timestamp. Whenever a new user was was wrongly identified as old,the creation and last sign in timestamp differed by 1.

Here's the logcat with timestamps:

2020-06-19 21:22:37.553 32080-32080/com.example.XX D/LoginRegisterActivity: onActivityResult: creationTimestamp 1592581957752
2020-06-19 21:22:37.553 32080-32080/com.example.XX D/LoginRegisterActivity: onActivityResult: lastsignin 1592581957753

please suggest something

Edit 1: I have observed one thing,if I have deleted a test number from console and also the corresponding user and then again add the same test number in console, I'm getting identified as old user. Are the test numbers stored even after being deleted? because when I change the test number to a completely random new number and then attempt to register, I'm correctly identified as new user

Edit 2: just now added a completely random test number in console,now tried to register user. I was taken to home instead of profile activity. Is this a bug? My code was working fine until I added this number

Attempt at alternate solution: I am now querying firestore for userID as suggested by someone. If userID exists , I'll treat it as an old user and send the user to Home. I want to ask is the userID unique for each phone number? Basically does each phoneNo correspond to a single user?

Chirag
  • 98
  • 8

1 Answers1

0

Instead of doing all these things You just need to use,

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // User is signed in
} else {
    // No user is signed in
}

check official document from below link.

doc

  • Please read my question again, I want to check if it's a new user or a returning user. I don't want to check if any user is logged in – Chirag Jun 19 '20 at 18:32
  • Then as soon as the user is going to enter his credentials you need to check whether data is exists or not by querying. You can refer https://stackoverflow.com/a/39025129/10042469 link for how to query in firebase – ABHIRAMAREDDY REDDY Jun 19 '20 at 18:49
  • I know how to query data. What is the problem with my code? this method is also mentioned in the official firebase ui github repo – Chirag Jun 19 '20 at 18:56