Firebase 9.0.0- How to see if user is logged in with AuthStateListener

倾然丶 夕夏残阳落幕 提交于 2019-12-21 03:19:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!