FireBase身份验证身份验证状态已更改 [英] FireBase Authentication Auth State Changed

查看:102
本文介绍了FireBase身份验证身份验证状态已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够根据用户的登录状态是否更改将意图传递给活动

I want to be able to pass an intent to an activity based on if the login state changes for the user

我必须遵循LoginActivity的OnCreate方法中的AuthStateListener

i have to following AuthStateListener within the OnCreate method of the LoginActivity

如果用户已登录,那么我希望他们被转发到MainActivity

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

但是,如果用户已注销,则需要进入LoginActivity

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

问题出在他们注销时,陷入了无限循环,并不断激发LoginActivity的意图.

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

当身份验证状态更改时,是否有任何方法可以告知用户位置(哪个活动).这样,我可以将签出的intent调用放在if语句中,以检查它们是否已经在LoginAcitvity上,从而防止循环

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");
            }
            // ...
        }
    };

推荐答案

答案最终还是很合逻辑的

The answer was fairly logical in the end

我将Firebase Auth调用移至我的主要活动,并修改了流程以将意图转发给Login活动,而不是相反.这样可以避免任何无限循环

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);

这篇关于FireBase身份验证身份验证状态已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆