简体   繁体   English

Firebase Auth无法使用Android登出

[英]Firebase Auth cannot sign out using Android

I am totally new to Android and firebase and I am having some issues when I try to sign out from Firebase. 我是Android和Firebase的新手,尝试退出Firebase时遇到一些问题。

A piece of my code is following at the bottom.. 我的代码片段位于底部。

In the onAuthStateChanged listener I try to create a Firebase user and get the current user in order to check if it is NOT null (which according to the Firebase docs means the user is signed in) or null which means the user is not signed in.. 在onAuthStateChanged侦听器中,我尝试创建一个Firebase用户并获取当前用户,以检查它是否为非null(根据Firebase文档,这表示该用户已登录)或null(表示该用户未登录)。 。

Then comes the problem.. 然后是问题。

Out of testing / curiosity purposes, if the user is signed in, I try to update the user profile and set a custom display name.. to see what happens. 出于测试/好奇的目的,如果用户已登录,我会尝试更新用户配置文件并设置自定义显示名称。以查看会发生什么。

It appears that after updating the profile and more specifically the line "user.updateProfile(profileUpdates);" 似乎在更新配置文件之后,更具体地说是在行“ user.updateProfile(profileUpdates);”之后 breaks the signout() function. 破坏signout()函数。

If I comment out the line "user.updateProfile(profileUpdates);" 如果我将“ user.updateProfile(profileUpdates);”行注释掉 the signout() function seems to be working fine! signout()函数似乎工作正常!

Can you point out what am I doing wrong? 您能指出我做错了什么吗?

Are there duplicate instances of the firebase user somewhere? 某个地方是否有重复的Firebase用户实例?

Thanks in advance! 提前致谢!

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth FireAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase mFireDatabase;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    FireAuth = FirebaseAuth.getInstance();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

            FirebaseUser user = firebaseAuth.getCurrentUser();

            if (user != null) {
                // already signed in
                UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                        .setDisplayName("SuperMan").build();
                user.updateProfile(profileUpdates); // This breaks it!
                signoutButton.setText(user.getDisplayName().toString());

            } else {
                // not signed in

            }

        }
    };

}

public void signin(String email, String password){

   FireAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            Log.d("myTEST","signInWithEmailAndPassword:complete:"+task.isSuccessful());
        }
    });
}
private void signout(){
    //FireAuth.getInstance().signOut();
    FireAuth.signOut();

}

public void getDisplayName(){

    if (FireAuth.getCurrentUser() !=null){
        UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                .setDisplayName("SuperMan").build();

        FireAuth.getCurrentUser().updateProfile(profileUpdates);
        signoutButton.setText(FireAuth.getCurrentUser().getUid().toString());
        if(FireAuth.getCurrentUser().isAnonymous()){
            signoutButton.setText("Anonymous");
        }

    }else{
        signoutButton.setText("empty..");
    }
}


@Override
public void onStart() {
    super.onStart();
    FireAuth.addAuthStateListener(mAuthListener);
}

@Override
public void onStop(){
    super.onStop();
    FireAuth.removeAuthStateListener(mAuthListener);
}


@Override
public void onClick(View v){
    switch (v.getId()){
        case R.id.signinButton:
            signin(loginEditText.getText().toString(),passwordEditText.getText().toString());
        break;
        case R.id.signoutButton:
            signout();
        break;
        case R.id.button3:
            getDisplayName();
            break;

    }

}}

A couple of observations that might explain the behavior you are seeing. 一些观察可能会解释您所看到的行为。

First, FirebaseUser.updateProfile() may require a roundtrip to the Firebase server and completes asynchronously. 首先, FirebaseUser.updateProfile()可能需要往返Firebase服务器并异步完成。 You can add a completion listener with a Log message to get an understanding of this: 您可以添加带有Log消息的完成侦听器,以了解这一点:

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
        .setDisplayName("SuperMan").build();
user.updateProfile(profileUpdates)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        Log.d(TAG, "onComplete: " + user.getDisplayName());
    }
});
Log.d(TAG, "Profile change requested");

Second, changing the user profile appears to cause the AuthStateListener to fire, which will cause your code to loop. 其次,更改用户配置文件似乎会导致AuthStateListener触发,这将导致您的代码循环。 Adding some Log statements would confirm this is happening. 添加一些Log语句将确认这种情况的发生。 I suspect the looping is interfering with the sign-out processing. 我怀疑循环会干扰签出处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM