如何确定 Firebase 用户是否使用 Facebook 身份验证登录 [英] How to determine if a Firebase user is signed in using facebook authentication
问题描述
我正在使用来自 google 的 firebase,但在用户身份验证方面遇到了一些问题.使用 facebook 登录后,我在 AuthStateListener 中获取 FirebaseUser,但如何检测此用户是通过 facebook 登录还是以其他方式登录?
I am using firebase from google and I have some trouble with user authentication. After logging with facebook I obtain FirebaseUser in AuthStateListener, but how can I detect if this user is logged via facebook or differently?
更新正如@Frank van Puffelen 所说 FirebaseAuth.getInstance().getCurrentUser().getProviderId()应该返回facebook",但在我的情况下它返回firebase".现在我无法弄清楚这种行为的原因是什么.当我得到 FacebookToken 时,我会做这样的事情:
UPDATE As @Frank van Puffelen said FirebaseAuth.getInstance().getCurrentUser().getProviderId() should return "facebook", but in my case it returns "firebase". Now I cannot figure out what's the reason of this behavior. When I got FacebookToken I do something like this:
AuthCredential credential = FacebookAuthProvider.getCredential(facebookToken.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// 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()) {
}
}
});
在那之后,在调用 onComplete() 方法之前,我的 AuthStateListener 获取用户哪个提供程序 ID 不是facebook",因为它应该是.难道我做错了什么?我按照官方谷歌 documentation
And afterthat before onComplete() method is called, my AuthStateListener gets user which provider id is not "facebook" as it should be. Am I doing something wrong? I followed official google documentation
推荐答案
在 3.x 版及更高版本中,单个用户可以使用多个提供程序登录.所以不再有单一提供者 ID 的概念.事实上,当你打电话时:
In version 3.x and later a single user can be signed in with multiple providers. So there is no longer the concept of a single provider ID. In fact when you call:
FirebaseAuth.getInstance().getCurrentUser().getProviderId()
它总是会返回firebase
.
要检测用户是否使用 Facebook 登录,您必须检查提供商数据:
To detect if the user was signed in with Facebook, you will have to inspect the provider data:
for (UserInfo user: FirebaseAuth.getInstance().getCurrentUser().getProviderData()) {
if (user.getProviderId().equals("facebook.com")) {
System.out.println("User is signed in with Facebook");
}
}
这篇关于如何确定 Firebase 用户是否使用 Facebook 身份验证登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!