如何禁用GOOGLE_SIGN_IN_API自动在用户退出后登录

时间:2018-04-18 20:52:51

标签: android firebase android-studio firebase-authentication

使用GoogleSignInApi,GoogleSignInClient时,如何允许用户使用其他帐户登录?

当用户通过"退出"退出应用程序时按钮,然后按"登录"按钮(打开谷歌登录客户端),他们立即登录他们以前输入的谷歌凭据。 (没有提示输入用户名/密码,或选择其他谷歌帐户的能力。)这实际上意味着用户永远不能使用与他们第一次输入的帐户不同的帐户登录应用程序。即使我完全擦除模拟器数据并重新开始,它仍会记住凭据并自动使用同一帐户登录!

登录代码(LoginActivity,OnCreate):

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

登录方法

private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

firebaseAuthWithGoogle(在mAuth中保存用户令牌)

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            //updateUI(user);
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Toast.makeText(context,"login Failed", Toast.LENGTH_LONG);
                            //updateUI(null);
                        }
                    }
                });
    }

退出按钮:

FirebaseAuth.getInstance().signOut();

我认为问题与GoogleSignInOptions.Builder或GoogleApiClient.Builder有关,但我一直无法找到合适的文档,提供了我可以用来配置这些项目的替代选项列表。

我只需要允许用户使用其他帐户登录!为什么它始终使用以前的帐户登录,我该如何解决?

1 个答案:

答案 0 :(得分:1)

here

回答

只需添加:

mGoogleApiClient.clearDefaultAccountAndReconnect(); 
获取firebase用户后