FireBase Authentication Auth State Changed

Deadly 提交于 2021-01-29 03:44:07

问题


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 LoginActivity

If the user is logged in then i want them to be forwarded to the MainActivity

However if the user is logged out then they need to goto the LoginActivity

The problem comes when they are signed out, it gets stuck in an infinite loop, constantly firing intent at the LoginActivity.

Is there any way of telling where the user is (which activity) when the auth state changes. That way i could place the signed out intent call within an if statement to check if they are already at the LoginAcitvity, thus preventing the loop

   mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = 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 out
                String className = this.getClass().getSimpleName();
                if (!(className == "LoginActivity")) {
                    Intent intent = new Intent(getBaseContext(), LoginActivity.class);
                    //intent.putExtra("EXTRA_SESSION_ID", sessionId);
                    startActivity(intent);
                }

                Log.d("LOG_Login", "onAuthStateChanged:signed_out");
            }
            // ...
        }
    };

回答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 = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = 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 out
                    String className = this.getClass().getName();
                    if (!(className == "LoginActivity")) {
                         Intent intent = new Intent(getBaseContext(), LoginActivity.class);
                       // intent.putExtra("EXTRA_SESSION_ID", sessionId);
                         startActivity(intent);
                    }

                    Log.d("LOG_Login", "onAuthStateChanged:signed_out");
                }
                // ...
            }
        };

        mAuth.addAuthStateListener(mAuthListener);


来源:https://stackoverflow.com/questions/37533745/firebase-authentication-auth-state-changed

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