Summary: Nested Classes | Methods | Inherited Methods | [Expand All]
public abstract class

# FirebaseAuth

extends Object

java.lang.Object
↳ | com.google.firebase.auth.FirebaseAuth

## Class Overview

The entry point of the Firebase Authentication SDK.

First, obtain an instance of this class by calling `getInstance()`.

Then, sign up or sign in a user with one of the following methods:

- `createUserWithEmailAndPassword(String, String)`
- `signInWithEmailAndPassword(String, String)`
- `signInWithCredential(AuthCredential)`
- `signInAnonymously()`
- `signInWithCustomToken(String)`

Finally, call `getCurrentUser()` to get a `FirebaseUser` object, which contains information about the signed-in user.

## Summary

Nested Classes
interface | FirebaseAuth.AuthStateListener | Listener called when there is a change in the authentication state.

Public Methods
void | addAuthStateListener(FirebaseAuth.AuthStateListener listener) Registers a listener to changes in the authentication state.
Task<AuthResult> | createUserWithEmailAndPassword(String email, String password) Tries to create a new user account with the given email address and password.
Task<ProviderQueryResult> | fetchProvidersForEmail(String email) Returns a list of authentication providers that can be used to sign in a given user (identified by its main email address).
FirebaseUser | getCurrentUser() Returns the currently signed-in `FirebaseUser` or null if there is none.
static FirebaseAuth | getInstance() Returns an instance of this class corresponding to the default `FirebaseApp` instance.
static FirebaseAuth | getInstance(FirebaseApp firebaseApp) Returns an instance of this class corresponding to the given `FirebaseApp` instance.
void | removeAuthStateListener(FirebaseAuth.AuthStateListener listener) Unregisters a listener to authentication changes.
Task<Void> | sendPasswordResetEmail(String email) Triggers the Firebase Authentication backend to send a password-reset email to the given email address, which must correspond to an existing user of your app.
Task<AuthResult> | signInAnonymously() Signs in the user anonymously without requiring any credential.
Task<AuthResult> | signInWithCredential(AuthCredential credential) Tries to sign in a user with the given `AuthCredential`.
Task<AuthResult> | signInWithCustomToken(String token) Tries to sign in a user with a given Custom Token.
Task<AuthResult> | signInWithEmailAndPassword(String email, String password) Tries to sign in a user with the given email address and password.
void | signOut() Signs out the current user and clears it from the disk cache.

[Expand] Inherited Methods
From class java.lang.Object
Object | clone()
boolean | equals(Object arg0)
void | finalize()
final Class<?> | getClass()
int | hashCode()
final void | notify()
final void | notifyAll()
String | toString()
final void | wait(long arg0, int arg1)
final void | wait(long arg0)
final void | wait()

## Public Methods

#### public void addAuthStateListener (FirebaseAuth.AuthStateListener listener)

Registers a listener to changes in the authentication state. There can be more than one listener registered at the same time for one or more `FirebaseAuth` instances.

The listeners call back in the UI thread, on the following events:

- Right after the listener has been registered
- When a user signs in
- When the current user signs out
- When the current user changes
- When there is a change in the current user's token

It is a recommended practice to always listen to sign-out events, as you may want to prompt the user to sign in again and maybe restrict the information or actions they have access to.

Use `removeAuthStateListener(AuthStateListener)` to unregister a listener.

##### See Also

- `FirebaseAuth.AuthStateListener`

#### public Task<AuthResult> createUserWithEmailAndPassword (String email, String password)

Tries to create a new user account with the given email address and password. If successful, it also signs the user in into the app.

Access the signed-in user with `getCurrentUser()`.

Upon successful completion, this operation triggers an `onAuthStateChanged(FirebaseAuth)` event in all registered `FirebaseAuth.AuthStateListener`s.

Important: you must enable Email & Password accounts in the Firebase console before you can use this method.

### Exceptions:

- `FirebaseAuthWeakPasswordException` thrown if the password is not strong enough
- `FirebaseAuthInvalidCredentialsException` thrown if the `email` address is malformed
- `FirebaseAuthUserCollisionException` thrown if there already exists an account with the given `email` address

##### Returns

- `Task` of `AuthResult` with the result of the operation

##### See Also

- `signInWithEmailAndPassword(String, String)`
- `signInWithCredential(AuthCredential)`
- `signInAnonymously()`
- `signInWithCustomToken(String)`

#### public Task<ProviderQueryResult> fetchProvidersForEmail (String email)

Returns a list of authentication providers that can be used to sign in a given user (identified by its main email address).

This method is useful when you support multiple authentication mechanisms if you want to implement an email-first authentication flow. It is also useful to resolve a `FirebaseAuthUserCollisionException` thrown on `signInWithCredential(AuthCredential)`.

### Exceptions:

- `FirebaseAuthInvalidCredentialsException` thrown if the `email` address is malformed

##### Parameters

email | the email address that identifies the user to fetch the providers from

##### Returns

- `Task` of `ProviderQueryResult` with the result of the operation

#### public FirebaseUser getCurrentUser ()

Returns the currently signed-in `FirebaseUser` or null if there is none.

Use `getCurrentUser() != null` to check if a user is signed in.

##### Returns

- the signed-in user or null

#### public static FirebaseAuth getInstance ()

Returns an instance of this class corresponding to the default `FirebaseApp` instance.

#### public static FirebaseAuth getInstance (FirebaseApp firebaseApp)

Returns an instance of this class corresponding to the given `FirebaseApp` instance.

#### public void removeAuthStateListener (FirebaseAuth.AuthStateListener listener)

Unregisters a listener to authentication changes.

##### See Also

- `FirebaseAuth.AuthStateListener`

#### public Task<Void> sendPasswordResetEmail (String email)

Triggers the Firebase Authentication backend to send a password-reset email to the given email address, which must correspond to an existing user of your app.

### Exceptions:

- `FirebaseAuthInvalidUserException` thrown if there is no user corresponding to the given `email` address

##### Returns

- `Task` to track completion of the sending operation

#### public Task<AuthResult> signInAnonymously ()

Signs in the user anonymously without requiring any credential.

This method creates a new account in your Firebase Authentication system, except in the case where there was already an anonymous user signed in into this app. Access the signed-in user with `getCurrentUser()`.

Upon successful completion, this operation triggers an `onAuthStateChanged(FirebaseAuth)` event in all registered `FirebaseAuth.AuthStateListener`s.

Anonymous users do not require any credential, and are useful in situations where you want to persist information about your users before asking them to sign in. For example, they may be useful when implementing a signed-out shopping cart in an e-commerce application.

Due to the unauthenticated nature of this kind of user, they are not transferrable across devices. In order to allow your app's users to keep their information, ask them to provide some other authentication credentials, and link them to the current user with `linkWithCredential(AuthCredential)`.

Important: you must enable Anonymous accounts in the Firebase console before being able to use them.

##### Returns

- `Task` of `AuthResult` with the result of the operation

##### See Also

- `signInWithEmailAndPassword(String, String)`
- `createUserWithEmailAndPassword(String, String)`
- `signInWithCredential(AuthCredential)`
- `signInWithCustomToken(String)`

#### public Task<AuthResult> signInWithCredential (AuthCredential credential)

Tries to sign in a user with the given `AuthCredential`.

Use this method to sign in a user into your Firebase Authentication system. First retrieve the `credential` either directly from the user, in case of `EmailAuthCredential`, or from a supported authentication SDK, such as Google Sign-In or Facebook. Later access the signed-in user with `getCurrentUser()`.

For all `AuthCredential` types except `EmailAuthCredential`, this method will create an account for the user in the case that it didn't exist before.

Important: you must configure the authentication providers in the Firebase console before you can use them.

### Exceptions:

- `FirebaseAuthInvalidUserException` thrown if the user account you are trying to sign in to has been disabled. Also thrown if `credential` is an `EmailAuthCredential` with an email address that does not correspond to an existing user.
- `FirebaseAuthInvalidCredentialsException` thrown if the `credential` is malformed or has expired. If `credential instanceof EmailAuthCredential` it will be thrown if the password is incorrect.
- `FirebaseAuthUserCollisionException` thrown if there already exists an account with the email address asserted by the `credential`. Resolve this case by calling `fetchProvidersForEmail(String)` and then asking the user to sign in using one of them.

##### Returns

- `Task` of `AuthResult` with the result of the operation

##### See Also

- `signInWithEmailAndPassword(String, String)`
- `createUserWithEmailAndPassword(String, String)`
- `signInAnonymously()`
- `signInWithCustomToken(String)`

#### public Task<AuthResult> signInWithCustomToken (String token)

Tries to sign in a user with a given Custom Token.

Use this method after you retrieve a Firebase Auth Custom Token from your server, to sign in a user into your Firebase Authentication system. Access the signed-in user with `getCurrentUser()`.

Upon successful completion, this operation triggers an `onAuthStateChanged(FirebaseAuth)` event in all registered `FirebaseAuth.AuthStateListener`s.

This operation might create an account if the `uid` specified in the token corresponds to a user without a record in the system.

Read how to use Custom Token authentication and the cases where it is useful in the guides.

### Exceptions:

- `FirebaseAuthInvalidCredentialsException` thrown if the `token` format is incorrect or if it corresponds to a different Firebase App

##### Returns

- `Task` of `AuthResult` with the result of the operation

##### See Also

- `signInWithEmailAndPassword(String, String)`
- `createUserWithEmailAndPassword(String, String)`
- `signInWithCredential(AuthCredential)`
- `signInAnonymously()`

#### public Task<AuthResult> signInWithEmailAndPassword (String email, String password)

Tries to sign in a user with the given email address and password.

Access the signed-in user with `getCurrentUser()`.

Upon successful completion, this operation triggers an `onAuthStateChanged(FirebaseAuth)` event in all registered `FirebaseAuth.AuthStateListener`s.

This is equivalent to calling `signInWithCredential(AuthCredential)` with an `EmailAuthCredential`.

Important: you must enable Email & Password accounts in the Firebase console before being able to use this method.

### Exceptions:

- `FirebaseAuthInvalidUserException` thrown if the user account corresponding to `email` does not exist or has been disabled
- `FirebaseAuthInvalidCredentialsException` thrown if the `password` is wrong

##### Returns

- `Task` of `AuthResult` with the result of the operation

##### See Also

- `createUserWithEmailAndPassword(String, String)`
- `signInWithCredential(AuthCredential)`
- `signInAnonymously()`
- `signInWithCustomToken(String)`

#### public void signOut ()

Signs out the current user and clears it from the disk cache.

Upon successful completion, this operation triggers an `onAuthStateChanged(FirebaseAuth)` event in all registered `FirebaseAuth.AuthStateListener`s.