Create a user
You create a new user in your Firebase project by calling the createUserWithEmailAndPassword
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 calling the getCurrentUser
method. If no user is signed in, getCurrentUser
returns null:
Kotlin
valuser=Firebase.auth.currentUserif(user!=null){// User is signed in}else{// No user is signed in}
Java
FirebaseUseruser=FirebaseAuth.getInstance().getCurrentUser();if(user!=null){// User is signed in}else{// No user is signed in}
There are some cases where getCurrentUser
will return a non-null FirebaseUser
but the underlying token is not valid. This can happen, for example, if the user was deleted on another device and the local token has not refreshed. In this case, you may get a valid user getCurrentUser
but subsequent calls to authenticated resources will fail.
getCurrentUser
might also return null
because the auth object has not finished initializing.
If you attach an AuthStateListener you will get a callback every time the underlying token state changes. This can be useful to react to edge cases like those mentioned above.
Get a user's profile
To get a user's profile information, use the accessor methods of an instance of FirebaseUser
. For example:
Kotlin
valuser=Firebase.auth.currentUseruser?.let{// Name, email address, and profile photo Urlvalname=it.displayNamevalemail=it.emailvalphotoUrl=it.photoUrl// Check if user's email is verifiedvalemailVerified=it.isEmailVerified// 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// FirebaseUser.getIdToken() instead.valuid=it.uid}
Java
FirebaseUseruser=FirebaseAuth.getInstance().getCurrentUser();if(user!=null){// Name, email address, and profile photo UrlStringname=user.getDisplayName();Stringemail=user.getEmail();UriphotoUrl=user.getPhotoUrl();// Check if user's email is verifiedbooleanemailVerified=user.isEmailVerified();// 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// FirebaseUser.getIdToken() instead.Stringuid=user.getUid();}
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 getProviderData
method. For example:
Kotlin
valuser=Firebase.auth.currentUseruser?.let{for(profileinit.providerData){// Id of the provider (ex: google.com)valproviderId=profile.providerId// UID specific to the providervaluid=profile.uid// Name, email address, and profile photo Urlvalname=profile.displayNamevalemail=profile.emailvalphotoUrl=profile.photoUrl}}
Java
FirebaseUseruser=FirebaseAuth.getInstance().getCurrentUser();if(user!=null){for(UserInfoprofile:user.getProviderData()){// Id of the provider (ex: google.com)StringproviderId=profile.getProviderId();// UID specific to the providerStringuid=profile.getUid();// Name, email address, and profile photo UrlStringname=profile.getDisplayName();Stringemail=profile.getEmail();UriphotoUrl=profile.getPhotoUrl();}}
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 updateProfile
method. For example:
Kotlin
valuser=Firebase.auth.currentUservalprofileUpdates=userProfileChangeRequest{displayName="Jane Q. User"photoUri=Uri.parse("https://example.com/jane-q-user/profile.jpg")}user!!.updateProfile(profileUpdates).addOnCompleteListener{task-> if(task.isSuccessful){Log.d(TAG,"User profile updated.")}}
Java
FirebaseUseruser=FirebaseAuth.getInstance().getCurrentUser();UserProfileChangeRequestprofileUpdates=newUserProfileChangeRequest.Builder().setDisplayName("Jane Q. User").setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg")).build();user.updateProfile(profileUpdates).addOnCompleteListener(newOnCompleteListener<Void>(){@OverridepublicvoidonComplete(@NonNullTask<Void>task){if(task.isSuccessful()){Log.d(TAG,"User profile updated.");}}});
Set a user's email address
You can set a user's email address with the updateEmail
method. For example:
Kotlin
valuser=Firebase.auth.currentUseruser!!.updateEmail("user@example.com").addOnCompleteListener{task-> if(task.isSuccessful){Log.d(TAG,"User email address updated.")}}
Java
FirebaseUseruser=FirebaseAuth.getInstance().getCurrentUser();user.updateEmail("user@example.com").addOnCompleteListener(newOnCompleteListener<Void>(){@OverridepublicvoidonComplete(@NonNullTask<Void>task){if(task.isSuccessful()){Log.d(TAG,"User email address updated.");}}});
Send a user a verification email
You can send an address verification email to a user with the sendEmailVerification
method. For example:
Kotlin
valuser=Firebase.auth.currentUseruser!!.sendEmailVerification().addOnCompleteListener{task-> if(task.isSuccessful){Log.d(TAG,"Email sent.")}}
Java
FirebaseAuthauth=FirebaseAuth.getInstance();FirebaseUseruser=auth.getCurrentUser();user.sendEmailVerification().addOnCompleteListener(newOnCompleteListener<Void>(){@OverridepublicvoidonComplete(@NonNullTask<Void>task){if(task.isSuccessful()){Log.d(TAG,"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.
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:
Kotlin
auth.setLanguageCode("fr")// To apply the default app language instead of explicitly setting it.// auth.useAppLanguage()
Java
auth.setLanguageCode("fr");// To apply the default app language instead of explicitly setting it.// auth.useAppLanguage();
Set a user's password
You can set a user's password with the updatePassword
method. For example:
Kotlin
valuser=Firebase.auth.currentUservalnewPassword="SOME-SECURE-PASSWORD"user!!.updatePassword(newPassword).addOnCompleteListener{task-> if(task.isSuccessful){Log.d(TAG,"User password updated.")}}
Java
FirebaseUseruser=FirebaseAuth.getInstance().getCurrentUser();StringnewPassword="SOME-SECURE-PASSWORD";user.updatePassword(newPassword).addOnCompleteListener(newOnCompleteListener<Void>(){@OverridepublicvoidonComplete(@NonNullTask<Void>task){if(task.isSuccessful()){Log.d(TAG,"User password updated.");}}});
Send a password reset email
You can send a password reset email to a user with the sendPasswordResetEmail
method. For example:
Kotlin
valemailAddress="user@example.com"Firebase.auth.sendPasswordResetEmail(emailAddress).addOnCompleteListener{task-> if(task.isSuccessful){Log.d(TAG,"Email sent.")}}
Java
FirebaseAuthauth=FirebaseAuth.getInstance();StringemailAddress="user@example.com";auth.sendPasswordResetEmail(emailAddress).addOnCompleteListener(newOnCompleteListener<Void>(){@OverridepublicvoidonComplete(@NonNullTask<Void>task){if(task.isSuccessful()){Log.d(TAG,"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.
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:
Kotlin
auth.setLanguageCode("fr")// To apply the default app language instead of explicitly setting it.// auth.useAppLanguage()
Java
auth.setLanguageCode("fr");// To apply the default app language instead of explicitly setting it.// 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:
Kotlin
valuser=Firebase.auth.currentUser!!user.delete().addOnCompleteListener{task-> if(task.isSuccessful){Log.d(TAG,"User account deleted.")}}
Java
FirebaseUseruser=FirebaseAuth.getInstance().getCurrentUser();user.delete().addOnCompleteListener(newOnCompleteListener<Void>(){@OverridepublicvoidonComplete(@NonNullTask<Void>task){if(task.isSuccessful()){Log.d(TAG,"User 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 and throws FirebaseAuthRecentLoginRequiredException
. When this happens, re-authenticate the user by getting new sign-in credentials from the user and passing the credentials to reauthenticate
. For example:
Kotlin
valuser=Firebase.auth.currentUser!!// Get auth credentials from the user for re-authentication. The example below shows// email and password credentials but there are multiple possible providers,// such as GoogleAuthProvider or FacebookAuthProvider.valcredential=EmailAuthProvider.getCredential("user@example.com","password1234")// Prompt the user to re-provide their sign-in credentialsuser.reauthenticate(credential).addOnCompleteListener{Log.d(TAG,"User re-authenticated.")}
Java
FirebaseUseruser=FirebaseAuth.getInstance().getCurrentUser();// Get auth credentials from the user for re-authentication. The example below shows// email and password credentials but there are multiple possible providers,// such as GoogleAuthProvider or FacebookAuthProvider.AuthCredentialcredential=EmailAuthProvider.getCredential("user@example.com","password1234");// Prompt the user to re-provide their sign-in credentialsuser.reauthenticate(credential).addOnCompleteListener(newOnCompleteListener<Void>(){@OverridepublicvoidonComplete(@NonNullTask<Void>task){Log.d(TAG,"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