简体   繁体   English

FirebaseAuth.getInstance()。getUid()-如何运作?

[英]FirebaseAuth.getInstance().getUid() - How does it work?

I have 3 questions about this: 我对此有3个问题:

FirebaseAuth.getInstance().getUid()
  1. According to spec, getInstance() returns instance of the FirebaseAuth class, but the getUid not function in this class. 根据规范,getInstance()返回FirebaseAuth类的实例,但getUid在此类中不起作用。 But it works. 但这有效。 How ? 怎么样 ?

  2. It seems that getUid generates unique ID if this is a new user but it is not written in the spec. 如果这是新用户,但getUid似乎会生成唯一ID,但未在规范中编写。 How does this function work? 此功能如何工作?

  3. When does it throw exception or return null ? 什么时候抛出异常或返回null?

Thanks, Alex 谢谢,亚历克斯

From the documentation 文档中

public abstract String getUid () 公共抽象字符串getUid()

Returns a user identifier as specified by the authentication provider. 返回认证提供者指定的用户标识符。 For example, if this object corresponds to a Google user, returns a Google user ID. 例如,如果此对象对应于Google用户,则返回Google用户ID。 For phone number accounts, the UID will be the normalized phone number in E.164 format. 对于电话号码帐户,UID将是E.164格式的规范化电话号码。

When does it throw an exception or return null? 什么时候抛出异常或返回null?

This is handled inside the singleton and inside the method getUid() , I couldn't find out any documentation about the throws 这是在单例内部和方法getUid() ,我找不到有关抛出的任何文档

In order to getUid() to work, you will need to first get the current user signed in to get the ID 为了使getUid()正常工作,您需要首先让当前用户登录以获得ID

FirebaseAuth.getInstance().getCurrentUser().getUid();

You can prevent a null by checking first if the current user is != null or just use the AuthListener to check it 您可以通过先检查当前用户是否为!= null或仅使用AuthListener对其进行检查来防止null

FirebaseAuth currentUser = FirebaseAuth.getInstance().getCurrentUser();

if(currentUser != null)
  String uid = currentUser.getUid();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM