Manage Users in Firebase

Create a user

You create a new user in your Firebase project by calling the createUser method or by signing in a user for the first time using a federated identity provider, such as Google Sign-In or Facebook Login.

You can also create new password-authenticated users from the Authentication section of the Firebase console, on the Users page.

Get the currently signed-in user

The recommended way to get the current user is by setting a listener on the Auth object:

Swift

handle=Auth.auth().addStateDidChangeListener{auth,userin// ...}

Objective-C

self.handle=[[FIRAuthauth]addAuthStateDidChangeListener:^(FIRAuth*_Nonnullauth,FIRUser*_Nullableuser){// ...}];

By using a listener, you ensure that the Auth object isn't in an intermediate state—such as initialization—when you get the current user.

You can also get the currently signed-in user by using the currentUser property. If a user isn't signed in, currentUser is nil:

Swift

ifAuth.auth().currentUser!=nil{// User is signed in.// ...}else{// No user is signed in.// ...}

Objective-C

if([FIRAuthauth].currentUser){// User is signed in.// ...}else{// No user is signed in.// ...}

Get a user's profile

To get a user's profile information, use the properties of an instance of FIRUser. For example:

Swift

letuser=Auth.auth().currentUserifletuser=user{// The user's ID, unique to the Firebase project.// Do NOT use this value to authenticate with your backend server,// if you have one. Use getTokenWithCompletion:completion: instead.letuid=user.uidletemail=user.emailletphotoURL=user.photoURLvarmultiFactorString="MultiFactor: "forinfoinuser.multiFactor.enrolledFactors{multiFactorString+=info.displayName??"[DispayName]"multiFactorString+=" "}// ...}

Objective-C

FIRUser*user=[FIRAuthauth].currentUser;if(user){// The user's ID, unique to the Firebase project.// Do NOT use this value to authenticate with your backend server,// if you have one. Use getTokenWithCompletion:completion: instead.NSString*email=user.email;NSString*uid=user.uid;NSMutableString*multiFactorString=[NSMutableStringstringWithFormat:@"MultiFactor: "];for(FIRMultiFactorInfo*infoinuser.multiFactor.enrolledFactors){[multiFactorStringappendString:info.displayName];[multiFactorStringappendString:@" "];}NSURL*photoURL=user.photoURL;// ...}

Get a user's provider-specific profile information

To get the profile information retrieved from the sign-in providers linked to a user, use the providerData property. For example:

Swift

letuserInfo=Auth.auth().currentUser?.providerData[indexPath.row]cell?.textLabel?.text=userInfo?.providerID// Provider-specific UIDcell?.detailTextLabel?.text=userInfo?.uid

Objective-C

id<FIRUserInfo>userInfo=[FIRAuthauth].currentUser.providerData[indexPath.row];cell.textLabel.text=[userInfoproviderID];// Provider-specific UIDcell.detailTextLabel.text=[userInfouid];

Update a user's profile

You can update a user's basic profile information—the user's display name and profile photo URL—with the UserProfileChangeRequest class. For example:

Swift

letchangeRequest=Auth.auth().currentUser?.createProfileChangeRequest()changeRequest?.displayName=displayNamechangeRequest?.commitChanges{errorin// ...}

Objective-C

FIRUserProfileChangeRequest*changeRequest=[[FIRAuthauth].currentUserprofileChangeRequest];changeRequest.displayName=userInput;[changeRequestcommitChangesWithCompletion:^(NSError*_Nullableerror){// ...}];

Set a user's email address

You can set a user's email address with the updateEmail method. For example:

Swift

Auth.auth().currentUser?.updateEmail(to:email){errorin// ...}

Objective-C

[[FIRAuthauth].currentUserupdateEmail:userInputcompletion:^(NSError*_Nullableerror){// ...}];

Send a user a verification email

You can send an address verification email to a user with the sendEmailVerificationWithCompletion: method. For example:

Swift

Auth.auth().currentUser?.sendEmailVerification{errorin// ...}

Objective-C

[[FIRAuthauth].currentUsersendEmailVerificationWithCompletion:^(NSError*_Nullableerror){// ...}];

You can customize the email template that is used in Authentication section of the Firebase console, on the Email Templates page. See Email Templates in Firebase Help Center.

It is also possible to pass state via a continue URL to redirect back to the app when sending a verification email.

Additionally you can localize the verification email by updating the language code on the Auth instance before sending the email. For example:

Swift

Auth.auth().languageCode="fr"// To apply the default app language instead of explicitly setting it.// Auth.auth().useAppLanguage()

Objective-C

[FIRAuthauth].languageCode=@"fr";// To apply the default app language instead of explicitly setting it.// [[FIRAuth auth] useAppLanguage];

Set a user's password

You can set a user's password with the updatePassword method. For example:

Swift

Auth.auth().currentUser?.updatePassword(to:password){errorin// ...}

Objective-C

[[FIRAuthauth].currentUserupdatePassword:userInputcompletion:^(NSError*_Nullableerror){// ...}];

Send a password reset email

You can send a password reset email to a user with the sendPasswordReset method. For example:

Swift

Auth.auth().sendPasswordReset(withEmail:email){errorin// ...}

Objective-C

[[FIRAuthauth]sendPasswordResetWithEmail:userInputcompletion:^(NSError*_Nullableerror){// ...}];

You can customize the email template that is used in Authentication section of the Firebase console, on the Email Templates page. See Email Templates in Firebase Help Center.

It is also possible to pass state via a continue URL to redirect back to the app when sending a password reset email.

Additionally you can localize the password reset email by updating the language code on the Auth instance before sending the email. For example:

Swift

Auth.auth().languageCode="fr"// To apply the default app language instead of explicitly setting it.// Auth.auth().useAppLanguage()

Objective-C

[FIRAuthauth].languageCode=@"fr";// To apply the default app language instead of explicitly setting it.// [[FIRAuth auth] useAppLanguage];

You can also send password reset emails from the Firebase console.

Delete a user

You can delete a user account with the delete method. For example:

Swift

letuser=Auth.auth().currentUseruser?.delete{errorinifleterror=error{// An error happened.}else{// Account deleted.}}

Objective-C

FIRUser*user=[FIRAuthauth].currentUser;[userdeleteWithCompletion:^(NSError*_Nullableerror){if(error){// An error happened.}else{// Account deleted.}}];

You can also delete users from the Authentication section of the Firebase console, on the Users page.

Re-authenticate a user

Some security-sensitive actions—such as deleting an account, setting a primary email address, and changing a password—require that the user has recently signed in. If you perform one of these actions, and the user signed in too long ago, the action fails with the FIRAuthErrorCodeCredentialTooOld error. When this happens, re-authenticate the user by getting new sign-in credentials from the user and passing the credentials to reauthenticate. For example:

Swift

letuser=Auth.auth().currentUservarcredential:AuthCredential// Prompt the user to re-provide their sign-in credentialsuser?.reauthenticate(with:credential){errorinifleterror=error{// An error happened.}else{// User re-authenticated.}}

Objective-C

FIRUser*user=[FIRAuthauth].currentUser;FIRAuthCredential*credential;// Prompt the user to re-provide their sign-in credentials[userreauthenticateWithCredential:credentialcompletion:^(NSError*_Nullableerror){if(error){// An error happened.}else{// User re-authenticated.}}];

Import user accounts

You can import user accounts from a file into your Firebase project by using the Firebase CLI's auth:import command. For example:

firebaseauth:importusers.json--hash-algo=scrypt--rounds=8--mem-cost=14