Firebase Authentication Auth State Changed
I want to be able to pass an intent to an activity based on if the login state changes for the user i have to following AuthStateListener within the OnCreate method of the LoginAct
Solution 1:
The answer was fairly logical in the end
I moved the Firebase Auth call to my main activity and amended the process to forward an intent to the Login activity rather than the other way round. This avoids any infinite loops
mAuthListener = newFirebaseAuth.AuthStateListener() {
@OverridepublicvoidonAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUseruser= firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in// Intent intent = new Intent(getBaseContext(), MainActivity.class);//intent.putExtra("EXTRA_SESSION_ID", sessionId);// startActivity(intent);// Log.d("LOG_Login", "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed outStringclassName=this.getClass().getName();
if (!(className == "LoginActivity")) {
Intentintent=newIntent(getBaseContext(), LoginActivity.class);
// intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);
}
Log.d("LOG_Login", "onAuthStateChanged:signed_out");
}
// ...
}
};
mAuth.addAuthStateListener(mAuthListener);
Post a Comment for "Firebase Authentication Auth State Changed"