gpt4 book ai didi

Android Firebase Facebook 身份验证注销功能

转载 作者:行者123 更新时间:2023-11-29 19:20:08 25 4
gpt4 key购买 nike

我正在将 facebook 身份验证方法与 firebase 一起使用,但我不知道如何正确注销用户。在我按下“继续使用 Facebook”按钮并允许它访问我的个人资料后,该按钮会更改为“注销”并在我单击它时显示一个对话框。问题是它实际上并没有将我注销并且状态仍然是登录。

我找到了 here如何在我的应用程序中从 Firebase 和 Facebook 注销用户,但我不知道将这两行放在哪里。我正在寻找注销功能或类似功能。

我找到了 here (different here)我的解决方案可能是什么,但它非常困惑并且无法理解。你能帮帮我吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
mAuth = FirebaseAuth.getInstance();

super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
setContentView(R.layout.activity_login);

mCallbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.e(TAG, "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}

@Override
public void onCancel() {
Log.e(TAG, "facebook:onCancel");
// ...
}

@Override
public void onError(FacebookException error) {
Log.e(TAG, "facebook:onError", error);
// ...
}
});

mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.e(TAG, "onAuthStateChanged:signed_in:" + user.getUid());

DatabaseReference myRef = database.getReference("Users");
myRef.addValueEventListener(new ValueEventListener() {
boolean userExists = false;
User userFirebase;

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot mydata : dataSnapshot.getChildren()){
userFirebase = mydata.getValue(User.class);

if(userFirebase.getEmail().equals(user.getEmail())){
Log.e(TAG, "User found in the database");
userExists = true;
Intent intent = new Intent(getBaseContext(), ActivityMain.class);
startActivity(intent);
break;
}
}

if (!userExists){
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("Users").push().setValue(new User(user.getDisplayName(),user.getEmail(),0,0));

Intent intent = new Intent(getBaseContext(), ActivityMain.class);
startActivity(intent);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
}); // checks if the user is in database and writes him if not

} else {
// User is signed out
Log.e(TAG, "onAuthStateChanged:signed_out");
}
}
};


}

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

@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}

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

// Pass the activity result back to the Facebook SDK
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}

private void handleFacebookAccessToken(AccessToken token) {
Log.e(TAG, "handleFacebookAccessToken:" + token);

AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.e(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.e(TAG, "signInWithCredential", task.getException());
Toast.makeText(getBaseContext(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}

// ...
}
});
}

最佳答案

您需要实现自己的按钮并使用上述 2 种方法添加和 OnClickListener :

Button logoutButton = (Button) findViewById(R.id.logout_button);
logoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
}
});

您不能使用 facebook 注销按钮,因为它只会让您退出 facebook,您还需要 firebase 注销。

关于Android Firebase Facebook 身份验证注销功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42699645/

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