2

I am building an app where the user has to register first (google or Email). First time users will be directed to an activity where he/she will have enter their personal-details and Returning users will be directed to profile activity. I am using firebase authentication and Database for this.

How do I check if the user has already registered and direct him to his profile activity when he logs-in using his google ID? Right Now whenever the user uses the google sign-in option they are always redirected to the personal-details activity even if they have registered before.

This is my main activity with google Sign-In

public class MainActivity extends AppCompatActivity implements View.OnClickListener,GoogleApiClient.OnConnectionFailedListener{
private Button mainRegister;
private TextView mainsignIn;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private String TAG="MAIN_ACTIVITY";
private static int RC_SIGN_IN=0;
private GoogleApiClient mGoogleApiClient;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mAuth = FirebaseAuth.getInstance();
    mainRegister=(Button)findViewById(R.id.mainregister);
    mainsignIn=(TextView)findViewById(R.id.mainSignIN);

   // googlebutton=(SignInButton)findViewById(R.id.mainGoogleSignin);


    //Listener
    mAuthListener=new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if(user != null){
                Log.d("AUTH", "User logged in: " + user.getEmail());

            }else{
                Log.d("AUTH", "User Logged out");
                }
        }
    };

    //google signIN
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();

    mGoogleApiClient= new GoogleApiClient.Builder(this)
            .enableAutoManage(this,this)
            .addApi(Auth.GOOGLE_SIGN_IN_API,gso)
            .build();

    mainRegister.setOnClickListener(this);
    mainsignIn.setOnClickListener(this);
    findViewById(R.id.mainGoogleSignin).setOnClickListener(this);
}

//googleSignIn Method
private void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            // Google Sign In was successful, authenticate with Firebase
            //uncomment this app crashes
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);


       } else {
           // Google Sign In failed, update UI appropriately
            Log.d(TAG,"Google login Failed");
        }
   }
}



//use this if app dosent work
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    //Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d("AUTH", "signInWithCredential:onComplete:" + task.isSuccessful());
                    finish();
                    startActivity(new Intent(getApplicationContext(),PersonalDetailsActivity.class));

                }
            });
}

@Override
public void onStart() {
    super.onStart();
   mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}

@Override
public void onClick(View v) {
        if(v==findViewById(R.id.mainGoogleSignin)){
            signIn();
            //finish();
            //startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
        }
        if(v==mainRegister){
            finish();
            startActivity(new Intent(getApplicationContext(),EmailRegisterActivity.class));
        }
        if(v==mainsignIn){
            finish();
            startActivity(new Intent(getApplicationContext(),SignInActivity.class));
        }

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(TAG,"Connection Failed");
}

}

dabreo
  • 53
  • 1
  • 9

1 Answers1

1

Why don't you save the google sign-in token in your local database and every time the user opens the application make a check for the user token (maybe in your splash) and if it is present it means there is already an existing user make an intent to the profile activity. Else personal-details activity.

Pavan
  • 767
  • 4
  • 18