gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:30:31 24 4
gpt4 key购买 nike

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

现在,新的 Activity (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();
}
// ...
}
});
}

最佳答案

But when I click on GoogleSignIn button again, the same user is again logged in.

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

I want the account selection menu to pop up once again.

要解决此问题,您需要退出 Firebase 和 Google 帐户。像下面这样的方法可以帮助您解决问题:

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

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

关于java - android - Firebase Google 身份验证用户未注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52771803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com