如何确定Firebase用户是否使用Facebook身份验证登录 [英] How to determine if a Firebase user is signed in using facebook authentication

查看:128
本文介绍了如何确定Firebase用户是否使用Facebook身份验证登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是谷歌的firebase,我有一些用户身份验证的麻烦。使用Facebook登录后,我在AuthStateListener中获取了FirebaseUser,但是如何检测到这个用户是通过Facebook登录还是以不同的方式登录?

更新
As @Frank van Puffelen说FirebaseAuth.getInstance()。getCurrentUser()。getProviderId()
应该返回Facebook,但在我的情况下,它返回firebase。现在我无法弄清楚这是什么原因。当我得到FacebookToken我做这样的事情:

  AuthCredential凭证= FacebookAuthProvider.getCredential(facebookToken.getToken()); 
mAuth.signInWithCredential(凭证)
.addOnCompleteListener(this,new OnCompleteListener< AuthResult>(){
@Override
public void onComplete(@NonNull Task< AuthResult> task){

//如果登录失败,向用户显示一条消息,如果登录成功
// auth状态侦听器将被通知,逻辑处理
//
if(!task.isSuccessful()){

}

}
});

在调用onComplete()方法之前,我的AuthStateListener获取用户的哪个提供者id不是facebook 正如它应当是的样子。我做错了什么?我遵循官方谷歌文档


  FirebaseAuth.getInstance()。getCurrentUser()。getProviderId()

它总是会返回 firebase



要检测用户是否使用Facebook登录,您必须检查提供者数据:

  for(UserInfo user:FirebaseAuth.getInstance()。getCurrentUser()。getProviderData()){
if(user.getProviderId()。equals(facebook.com)){
System.out。 println(用户使用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?

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()) {

                        }

                    }
                });

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

解决方案

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()

It will always return firebase.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆