android-Firebase Google身份验证用户未注销

时间:2018-10-12 03:27:01

标签: java android firebase firebase-realtime-database firebase-authentication

我的MainActivity包含一个GoogleSignIn按钮,该按钮会弹出一个带有设备上所有Google帐户的菜单。一切正常。用户能够成功登录并定向到新的活动。

现在,新的活动(Main2Activity)包含一个注销按钮,该按钮将用户再次重定向到MainActivity。但是,当我再次单击GoogleSignIn按钮时,同一用户再次登录。我希望再次弹出帐户选择菜单。如果用户想使用其他帐户登录怎么办? 这是我在Main2Activity中使用的退出代码:

HomeActivity / Main2Activity

findViewById(R.id.logoutButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            firebaseAuth.signOut();

            startActivity(new Intent(getApplicationContext(), LoginActivity.class));
            finish();
        }
    });
}

LoginActivity / MainActivity

package com.dell.nfclib;



public class LoginActivity extends Activity
{
    private static final int RC_SIGN_IN = 101;
    GoogleSignInClient mGoogleSignInClient;
private FirebaseAuth mAuth;
SignInButton signInButton;

@Override
protected void onStart()
{
    super.onStart();

}

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

    // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
    // Build a GoogleSignInClient with the options specified by gso.
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    // Initialize Firebase Auth
    mAuth = FirebaseAuth.getInstance();
    signInButton = (SignInButton) findViewById(R.id.googleSignInButton);
    signInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signIn();
        }
    });
}

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

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {

        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if(result.isSuccess()) {
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);


        }
    }
}

private void firebaseAuthWithGoogle(GoogleSignInAccount acct)
{
    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
                        // Get user details from the 'user' object..


                        startActivity(new Intent(getApplicationContext(), HomeActivity.class));
                        finish();
                    }
                    else
                    {
                        // If sign in fails, display a message to the user.

                        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
                    }
                    // ...
                }
            });
}

}

1 个答案:

答案 0 :(得分:1)

  

但是当我再次单击GoogleSignIn按钮时,同一用户再次登录。

之所以发生这种情况,是因为您尚未完全退出。

  

我希望再次弹出帐户选择菜单。

要解决此问题,您需要同时退出Firebase和Google帐户。如下所示的方法可以帮助您解决问题:

private void signOut() {
    FirebaseFirestore.getInstance().signOut(); //Sign-out Firebase

    if (googleApiClient.isConnected()) {
        Auth.GoogleSignInApi.signOut(googleApiClient); //Sign-out Google
    }
}