0

My requirement is to differentiate between first time and subsequent Google-sign in in my android application. For first time user google sign-in, I want to redirect the user to the profile screen, where he will fill additional information regarding his contact details. After his first google sign in, all the subsequent google sign-in, I want the user to be directed directly to the home screen, without filling out the Profile details once again. I use Firebase for my backend. I have followed a couple of stack overflow questions and providing the link for the same below.

Link 1: Correct code location to check Firebase if a user has been created already?

Link 2: How to differentiate between first time google Sign-In and successive google sign-In in an android application?

I was able to achieve my requirement with a simple bug, which I couldn't figure out what I am missing. First time Google sign-in directs to Profile screen and subsequent requests redirects to Home screen. But what I tried, it happens only until the user closes the app. If the user closes the application and opens again, the user is redirected to the profile screen, which shouldn't happen. Once the user signs in with Google and has registered with the Profile, all the subsequent requests should go to Home screen.

My Bug: When the user closes the app and reopens again, the user is redirected to the Profile screen on google sign in. Here is what I have tried

SignInActivity.java

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(SignInActivity.this);
if (firebaseAuth.getCurrentUser() != null) {
    if (!sharedPreferences.contains(String.valueOf(Preferences.FIRST_LAUNCH))) {
        Intent contactDetailsIntent = new Intent(SignInActivity.this, ContactDetailsActivity.class);
        startActivity(contactDetailsIntent);
        sharedPreferences.edit().putBoolean(String.valueOf(Preferences.FIRST_LAUNCH), true).commit();
    } else {
        Intent loginIntent = new Intent(SignInActivity.this,MainMenuActivity.class);
        startActivity(loginIntent);
        finish();
    }
}

Here is my preferences class.

public class Preferences {

    public static final boolean FIRST_LAUNCH = false;
    public static final String NAME = "fullName";
    public static final String EMAIL = "email";
    public static final String USERID = "userId";
    public static final String USER_TYPE = "userType";

}

I am unable to figure out what the issue is. Any help is appreciated

Ramji Seetharaman
  • 524
  • 4
  • 7
  • 24

1 Answers1

0

To check if the user is logged in on their device, try this Firebase 9.0.0- How to see if user is logged in with AuthStateListener I have used it for email authentication, but since it is linked to the user's account on firebse, I don't think it will matter. Also, I suggest putting it in the onCreate method.

To check for the number of times the user has logged in on their device, use firebase database. Get the Unique ID of the user using:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserID = user.getUid();

I would then check the database to see if the user with that unique ID exists. If it doesn't then store the ID in the database, create a child "timesLoggedIn" or something like that, and give it the value 0. If it does exist, then increment "timesLoggedIn".

Mike
  • 23
  • 8