Ir para o console

FirebaseAuth

public abstract class FirebaseAuth extends Object

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:

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

Nested Class Summary

interface FirebaseAuth.AuthStateListener Listener called when there is a change in the authentication state. 
interface FirebaseAuth.IdTokenListener Listener called when the id token is changed. 

Public Method Summary

void
addAuthStateListener(FirebaseAuth.AuthStateListener listener)
Registers a listener to changes in the user authentication state.
void
addIdTokenListener(FirebaseAuth.IdTokenListener listener)
Registers a listener to changes in the token authentication state.
Task<Void>
applyActionCode(String code)
Applies the given code, which can be any out of band code which is valid according to checkActionCode(String) that does not also pass verifyPasswordResetCode(String), which requires an additional parameter.
Task<ActionCodeResult>
checkActionCode(String code)
Checks that the code given is valid.
Task<Void>
confirmPasswordReset(String code, String newPassword)
Changes the user's password to newPassword for the account for which the code is valid.
Task<AuthResult>
createUserWithEmailAndPassword(String email, String password)
Tries to create a new user account with the given email address and password.
Task<SignInMethodQueryResult>
fetchSignInMethodsForEmail(String email)
Returns a list of signin methods that can be used to sign in a given user (identified by its main email address).
FirebaseApp
getApp()
Returns the FirebaseApp instance to which this FirebaseAuth belongs.
FirebaseUser
getCurrentUser()
Returns the currently signed-in FirebaseUser or null if there is none.
FirebaseAuthSettings
getFirebaseAuthSettings()
Returns the FirebaseAuthSettings instance for this FirebaseAuth instance.
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.
String
getLanguageCode()
Returns the language code set in setLanguageCode(String).
Task<AuthResult>
getPendingAuthResult()
Returns a Task wrapping an AuthResult.
boolean
isSignInWithEmailLink(String link)
Determines if the given link is a link intended for use with getCredentialWithLink(String, String).
void
removeAuthStateListener(FirebaseAuth.AuthStateListener listener)
Unregisters a listener to authentication changes.
void
removeIdTokenListener(FirebaseAuth.IdTokenListener listener)
Unregisters a listener to authentication changes.
Task<Void>
sendPasswordResetEmail(String email, ActionCodeSettings actionCodeSettings)
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<Void>
Task<Void>
sendSignInLinkToEmail(String email, ActionCodeSettings actionCodeSettings)
Sends an email to the specified email which will contain a link to be used to sign in the user.
Task<Void>
setFirebaseUIVersion(String firebaseUIVersion)
For internal use in FirebaseUI only.
void
setLanguageCode(String languageCode)
Sets the user-facing language code for auth operations that can be internationalized, such as sendEmailVerification().
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.
Task<AuthResult>
signInWithEmailLink(String email, String link)
Tries to sign in a user with the given email address and link.
void
signOut()
Signs out the current user and clears it from the disk cache.
Task<AuthResult>
startActivityForSignInWithProvider(Activity activity, FederatedAuthProvider federatedAuthProvider)
Signs in the user using the mobile browser (either a Custom Chrome Tab or the device's default browser) for the given provider.
Task<Void>
updateCurrentUser(FirebaseUser user)
Sets the current user to a copy of the given user, but associated with this FirebaseAuth's FirebaseApp.
void
useAppLanguage()
Sets the user-facing language code to be the default app language.
Task<String>
verifyPasswordResetCode(String code)
Checks that the code is a valid password reset out of band code.

Inherited Method Summary

Public Methods

public void addAuthStateListener (FirebaseAuth.AuthStateListener listener)

Registers a listener to changes in the user 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

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 addIdTokenListener(IdTokenListener) if you want to listen to token refreshes.

See Also: FirebaseAuth.AuthStateListener

public void addIdTokenListener (FirebaseAuth.IdTokenListener listener)

Registers a listener to changes in the token 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 removeIdTokenListener(IdTokenListener) to unregister a listener.

See addAuthStateListener(AuthStateListener) if you do not want to listen to token refreshes.

See Also: FirebaseAuth.IdTokenListener

public Task<Void> applyActionCode (String code)

Applies the given code, which can be any out of band code which is valid according to checkActionCode(String) that does not also pass verifyPasswordResetCode(String), which requires an additional parameter.

public Task<ActionCodeResult> checkActionCode (String code)

Checks that the code given is valid. This code will have been generated by sendPasswordResetEmail(String) or sendEmailVerification() valid for a single use.

Returns

public Task<Void> confirmPasswordReset (String code, String newPassword)

Changes the user's password to newPassword for the account for which the code is valid. Code validity can be checked with verifyPasswordResetCode(String). This use case is only valid for signed-out users, and behavior is undefined for signed-in users. Password changes for signed-in users should be made using updatePassword(String).

Exceptions

See Also:

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 onIdTokenChanged(FirebaseAuth) event in all registered FirebaseAuth.IdTokenListeners and an onAuthStateChanged(FirebaseAuth) event in all registered FirebaseAuth.AuthStateListeners.

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

Exceptions

See Also:

Returns

public Task<SignInMethodQueryResult> fetchSignInMethodsForEmail (String email)

Returns a list of signin methods that can be used to sign in a given user (identified by its main email address). This will match the getSignInMethod() for the credential you would generate for the appropriate signin mechanism.

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
Parameters
email the email address that identifies the user to fetch the providers from
Returns

public FirebaseApp getApp ()

Returns the FirebaseApp instance to which this FirebaseAuth belongs.

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 FirebaseAuthSettings getFirebaseAuthSettings ()

Returns the FirebaseAuthSettings instance for this FirebaseAuth instance.

public static FirebaseAuth getInstance ()

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

Note: Firebase Authentication does not currently support Android Direct Boot.

For Applications that use Direct Boot, please check if android.content.Context.isDeviceProtectedStorage is false before you call FirebaseAuth.getInstance().

public static FirebaseAuth getInstance (FirebaseApp firebaseApp)

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

Note: Firebase Authentication does not currently support Android Direct Boot.

For Applications that use Direct Boot, please check if android.content.Context.isDeviceProtectedStorage is false before you call FirebaseAuth.getInstance(firebaseApp).

public String getLanguageCode ()

Returns the language code set in setLanguageCode(String).

public Task<AuthResult> getPendingAuthResult ()

Returns a Task wrapping an AuthResult. This will return a non-null value if your app launches a web sign-in flow and the OS cleans up your hosting Activity while in the background (likely due to a low-memory event). The returned result is the value that startActivityForSignInWithProvider(Activity, FederatedAuthProvider) would have returned, which includes any Exceptions thrown. Otherwise, returns null.

This method will only return a non-null result for a sign-in once. A non-null value will only be present for suitably recent sign-ins to help prevent false positive sign-ins.

public boolean isSignInWithEmailLink (String link)

Determines if the given link is a link intended for use with getCredentialWithLink(String, String). These links are generated by sendSignInLinkToEmail(String, ActionCodeSettings).

public void removeAuthStateListener (