Create a user
You create a new user in your Firebase project by calling the
createUserWithEmail:password:completion:
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:
Objective-C
[[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth,
FIRUser *_Nullable user) {
if (user != nil) {
// User is signed in.
} else {
// No user is signed in.
}
}];
Swift
FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
if let user = user {
// User is signed in.
} else {
// No user is signed in.
}
}
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:
Objective-C
FIRUser *user = [FIRAuth auth].currentUser;
if (user != nil) {
// User is signed in.
} else {
// No user is signed in.
}
Swift
if let user = FIRAuth.auth()?.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:
Objective-C
FIRUser *user = [FIRAuth auth].currentUser;
if (user != nil) {
NSString *name = user.displayName;
NSString *email = user.email;
NSURL *photoUrl = user.photoURL;
NSString *uid = user.uid; // 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.
} else {
// No user is signed in.
}
Swift
if let user = FIRAuth.auth()?.currentUser {
let name = user.displayName
let email = user.email
let photoUrl = user.photoURL
let uid = user.uid; // 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.
} else {
// No user is signed in.
}
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:
Objective-C
FIRUser *user = [FIRAuth auth].currentUser;
if (user != nil) {
for (FIRUserInfo *profile in user.providerData) {
NSString *providerID = profile.providerID;
NSString *uid = profile.uid; // Provider-specific UID
NSString *name = profile.displayName;
NSString *email = profile.email;
NSURL *photoURL = profile.photoURL;
}
} else {
// No user is signed in.
}
Swift
if let user = FIRAuth.auth()?.currentUser {
for profile in user.providerData {
let providerID = profile.providerID
let uid = profile.uid; // Provider-specific UID
let name = profile.displayName
let email = profile.email
let photoURL = profile.photoURL
}
} else {
// No user is signed in.
}
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 FIRUserProfileChangeRequest
class. For
example:
Objective-C
FIRUser *user = [FIRAuth auth].currentUser;
FIRUserProfileChangeRequest *changeRequest = [user profileChangeRequest];
changeRequest.displayName = @"Jane Q. User";
changeRequest.photoURL =
[NSURL URLWithString:@"https://example.com/jane-q-user/profile.jpg"];
[changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) {
if (error) {
// An error happened.
} else {
// Profile updated.
}
}];
Swift
let user = FIRAuth.auth()?.currentUser
if let user = user {
let changeRequest = user.profileChangeRequest()
changeRequest.displayName = "Jane Q. User"
changeRequest.photoURL =
NSURL(string: "https://example.com/jane-q-user/profile.jpg")
changeRequest.commitChangesWithCompletion { error in
if let error = error {
// An error happened.
} else {
// Profile updated.
}
}
}
Set a user's email address
You can set a user's email address with the updateEmail:completion:
method.
For example:
Objective-C
FIRUser *user = [FIRAuth auth].currentUser;
[user updateEmail:@"user@example.com" completion:^(NSError *_Nullable error) {
if (error) {
// An error happened.
} else {
// Email updated.
}
}];
Swift
let user = FIRAuth.auth()?.currentUser
user?.updateEmail("user@example.com") { error in
if let error = error {
// An error happened.
} else {
// Email updated.
}
}
Send a user a verification email
You can send an address verification email to a user with the
sendEmailVerificationWithCompletion:
method. For example:
Objective-C
FIRUser *user = [FIRAuth auth].currentUser;
[user sendEmailVerificationWithCompletion:^(NSError *_Nullable error) {
if (error) {
// An error happened.
} else {
// Email sent.
}
}];
Swift
let user = FIRAuth.auth()?.currentUser
user?.sendEmailVerificationWithCompletion() { error in
if let error = error {
// An error happened.
} else {
// Email sent.
}
}
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.
Set a user's password
You can set a user's password with the updatePassword:completion:
method. For
example:
Objective-C
FIRUser *user = [FIRAuth auth].currentUser;
NSString *newPassword = [yourApp getRandomSecurePassword];
[user updatePassword:newPassword completion:^(NSError *_Nullable error) {
if (error) {
// An error happened.
} else {
// Password updated.
}
}];
Swift
let user = FIRAuth.auth()?.currentUser
let newPassword = getRandomSecurePassword()
user?.updatePassword(newPassword) { error in
if let error = error {
// An error happened.
} else {
// Password updated.
}
}
Send a password reset email
You can send a password reset email to a user with the
sendPasswordResetWithEmail:completion:
method. For example:
Objective-C
NSString *email = @"user@example.com";
[[FIRAuth auth] sendPasswordResetWithEmail:email
completion:^(NSError *_Nullable error) {
if (error) {
// An error happened.
} else {
// Password reset email sent.
}
}];
Swift
let email = "user@example.com"
FIRAuth.auth()?.sendPasswordResetWithEmail(email) { error in
if let error = error {
// An error happened.
} else {
// Password reset email sent.
}
}
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.
You can also send password reset emails from the Firebase console.
Delete a user
You can delete a user account with the deleteWithCompletion
method. For
example:
Objective-C
FIRUser *user = [FIRAuth auth].currentUser;
[user deleteWithCompletion:^(NSError *_Nullable error) {
if (error) {
// An error happened.
} else {
// Account deleted.
}
}];
Swift
let user = FIRAuth.auth()?.currentUser
user?.deleteWithCompletion { error in
if let error = 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:
Objective-C
FIRUser *user = [FIRAuth auth].currentUser;
FIRAuthCredential *credential;
// Prompt the user to re-provide their sign-in credentials
[user reauthenticateWithCredential:credential completion:^(NSError *_Nullable error) {
if (error) {
// An error happened.
} else {
// User re-authenticated.
}
}];
Swift
let user = FIRAuth.auth()?.currentUser
var credential: FIRAuthCredential
// Prompt the user to re-provide their sign-in credentials
user?.reauthenticateWithCredential(credential) { error in
if let error = error {
// An error happened.
} else {
// User re-authenticated.
}
}