问题
With Google's Firebase AuthStateListener, I'm not sure how to detect if the Auth token has expired. I'm using email and password authentication and the listener works correctly for the first time users open the app and logs them in automatically on subsequent app launches.
Problem arises when their login token expires and the user should have to re-authenticate. The AuthStateListener just logs them in, but the Firebase service denies them access because their token has expired.
My Listener Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selector);
mRef = FirebaseDatabase.getInstance().getReference();
auth = FirebaseAuth.getInstance();
firebaseAuth = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser()!= null){
Toast.makeText(getApplicationContext(), "Logged in", Toast.LENGTH_SHORT).show();
changeToNextFragment();
} else {
Toast.makeText(getApplicationContext(), "You are not currently logged in.", Toast.LENGTH_SHORT).show();
changeToLogin();
}
}
};
And the listener is called here:
@Override
protected void onStart() {
super.onStart();
auth.addAuthStateListener(firebaseAuth);
}
What is the appropriate test on the FirebaseAuth Object to see if the token has expired?
来源:https://stackoverflow.com/questions/37367971/firebase-9-0-0-how-to-see-if-user-is-logged-in-with-authstatelistener