简体   繁体   中英

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

I have 3 questions about this:

FirebaseAuth.getInstance().getUid()
  1. According to spec, getInstance() returns instance of the FirebaseAuth class, but the getUid not function in this class. 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. How does this function work?

  3. When does it throw exception or return null ?

Thanks, Alex

From the documentation

public abstract String 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. For phone number accounts, the UID will be the normalized phone number in E.164 format.

When does it throw an exception or return null?

This is handled inside the singleton and inside the method getUid() , I couldn't find out any documentation about the throws

In order to getUid() to work, you will need to first get the current user signed in to get the 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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