FIRAuth


@interface FIRAuth : NSObject

Manages authentication for Firebase apps. This class is thread-safe.

  • Gets the auth object for the default Firebase app. The default Firebase app must have already been configured or an exception will be raised.

    Declaration

    Swift

    class func auth() -> Auth

    Objective-C

    + (nonnull FIRAuth *)auth;
  • Gets the auth object for a FIRApp.

    Declaration

    Swift

    class func auth(app: FIRApp) -> Auth

    Objective-C

    + (nonnull FIRAuth *)authWithApp:(nonnull FIRApp *)app;

    Parameters

    app

    The FIRApp for which to retrieve the associated FIRAuth instance.

    Return Value

    The FIRAuth instance associated with the given FIRApp.

  • app

    Gets the FIRApp object that this auth object is connected to.

    Declaration

    Swift

    weak var app: FIRApp? { get }

    Objective-C

    @property (readonly, nonatomic, nullable) FIRApp *app;
  • Synchronously gets the cached current user, or null if there is none.

    Declaration

    Swift

    var currentUser: FIRUser? { get }

    Objective-C

    @property (readonly, strong, nonatomic, nullable) FIRUser *currentUser;
  • The APNs token used for phone number authentication. The type of the token (production or sandbox) will be attempted to be automatcially detected. If swizzling is disabled, the APNs Token must be set for phone number auth to work, by either setting this property or by calling setAPNSToken:type:

    Declaration

    Swift

    var apnsToken: Data? { get set }

    Objective-C

    @property (readwrite, strong, nonatomic, nullable) NSData *APNSToken;
  • Please access auth instances using FIRAuth.auth and FIRAuth.authForApp:.

    Declaration

    Objective-C

    - (nonnull instancetype)init;
  • Fetches the list of IdPs that can be used for signing in with the provided email address. Useful for an identifier-first sign-in flow.

    Possible error codes:

    • FIRAuthErrorCodeInvalidEmail - Indicates the email address is malformed.

    See FIRAuthErrors for a list of error codes that are common to all API methods.

    Declaration

    Swift

    func fetchProviders(forEmail email: String, completion: ProviderQueryCallback? = nil)

    Objective-C

    - (void)fetchProvidersForEmail:(nonnull NSString *)email
                        completion:(nullable FIRProviderQueryCallback)completion;

    Parameters

    email

    The email address for which to obtain a list of identity providers.

    completion

    Optionally; a block which is invoked when the list of providers for the specified email address is ready or an error was encountered. Invoked asynchronously on the main thread in the future.

  • Signs in using an email address and password.

    Possible error codes:

    • FIRAuthErrorCodeOperationNotAllowed - Indicates that email and password accounts are not enabled. Enable them in the Auth section of the Firebase console.
    • FIRAuthErrorCodeUserDisabled - Indicates the user’s account is disabled.
    • FIRAuthErrorCodeWrongPassword - Indicates the user attempted sign in with an incorrect password.
    • FIRAuthErrorCodeInvalidEmail - Indicates the email address is malformed.

    See FIRAuthErrors for a list of error codes that are common to all API methods.

    Declaration

    Swift

    func signIn(withEmail email: String, password: String, completion: AuthResultCallback? = nil)

    Objective-C

    - (void)signInWithEmail:(nonnull NSString *)email
                   password:(nonnull NSString *)password
                 completion:(nullable FIRAuthResultCallback)completion;

    Parameters

    email

    The user’s email address.

    password

    The user’s password.

    completion

    Optionally; a block which is invoked when the sign in flow finishes, or is canceled. Invoked asynchronously on the main thread in the future.

  • Convenience method for signInAndRetrieveDataWithCredential:completion: This method doesn’t return additional identity provider data.

    Declaration

    Swift

    func signIn(with credential: FIRAuthCredential, completion: AuthResultCallback? = nil)

    Objective-C

    - (void)signInWithCredential:(nonnull FIRAuthCredential *)credential
                      completion:(nullable FIRAuthResultCallback)completion;
  • Asynchronously signs in to Firebase with the given 3rd-party credentials (e.g. a Facebook login Access Token, a Google ID Token/Access Token pair, etc.) and returns additional identity provider data.

    Possible error codes:

    • FIRAuthErrorCodeInvalidCredential - Indicates the supplied credential is invalid. This could happen if it has expired or it is malformed.
    • FIRAuthErrorCodeOperationNotAllowed - Indicates that accounts with the identity provider represented by the credential are not enabled. Enable them in the Auth section of the Firebase console.
    • FIRAuthErrorCodeAccountExistsWithDifferentCredential - Indicates the email asserted by the credential (e.g. the email in a Facebook access token) is already in use by an existing account, that cannot be authenticated with this sign-in method. Call fetchProvidersForEmail for this user’s email and then prompt them to sign in with any of the sign-in providers returned. This error will only be thrown if the One account per email address setting is enabled in the Firebase console, under Auth settings.
    • FIRAuthErrorCodeUserDisabled - Indicates the user’s account is disabled.
    • FIRAuthErrorCodeWrongPassword - Indicates the user attempted sign in with an incorrect password, if credential is of the type EmailPasswordAuthCredential.
    • FIRAuthErrorCodeInvalidEmail - Indicates the email address is malformed.

    See FIRAuthErrors for a list of error codes that are common to all API methods.

    Declaration

    Swift

    func signInAndRetrieveData(with credential: FIRAuthCredential, completion: AuthDataResultCallback? = nil)

    Objective-C

    - (void)
    signInAndRetrieveDataWithCredential:(nonnull FIRAuthCredential *)credential
                             completion:
                                 (nullable FIRAuthDataResultCallback)completion;

    Parameters

    credential

    The credential supplied by the IdP.

    completion

    Optionally; a block which is invoked when the sign in flow finishes, or is canceled. Invoked asynchronously on the main thread in the future.

  • Asynchronously creates and becomes an anonymous user. - parameter: completion Optionally; a block which is invoked when the sign in finishes, or is canceled. Invoked asynchronously on the main thread in the future.

    If there is already an anonymous user signed in, that user will be returned instead. If there is any other existing user signed in, that user will be signed out.

    Possible error codes:

    • FIRAuthErrorCodeOperationNotAllowed - Indicates that anonymous accounts are not enabled. Enable them in the Auth section of the Firebase console.

    See FIRAuthErrors for a list of error codes that are common to all API methods.

    Declaration

    Swift

    func signInAnonymously(completion: AuthResultCallback? = nil)

    Objective-C

    - (void)signInAnonymouslyWithCompletion:
        (nullable FIRAuthResultCallback)completion;

    Parameters

    completion

    Optionally; a block which is invoked when the sign in finishes, or is canceled. Invoked asynchronously on the main thread in the future.

  • Asynchronously signs in to Firebase with the given Auth token.

    Possible error codes:

    • FIRAuthErrorCodeInvalidCustomToken - Indicates a validation error with the custom token.
    • FIRAuthErrorCodeCustomTokenMismatch - Indicates the service account and the API key belong to different projects.

    See FIRAuthErrors for a list of error codes that are common to all API methods.

    Declaration

    Swift

    func signIn(withCustomToken token: String, completion: AuthResultCallback? = nil)

    Objective-C

    - (void)signInWithCustomToken:(nonnull NSString *)token
                       completion:(nullable FIRAuthResultCallback)completion;

    Parameters

    token

    A self-signed custom auth token.

    completion

    Optionally; a block which is invoked when the sign in finishes, or is canceled. Invoked asynchronously on the main thread in the future.

  • Creates and, on success, signs in a user with the given email address and password.

    Possible error codes:

    • FIRAuthErrorCodeInvalidEmail - Indicates the email address is malformed.
    • FIRAuthErrorCodeEmailAlreadyInUse - Indicates the email used to attempt sign up already exists. Call fetchProvidersForEmail to check which sign-in mechanisms the user used, and prompt the user to sign in with one of those.
    • FIRAuthErrorCodeOperationNotAllowed - Indicates that email and password accounts are not enabled. Enable them in the Auth section of the Firebase console.
    • FIRAuthErrorCodeWeakPassword - Indicates an attempt to set a password that is considered too weak. The NSLocalizedFailureReasonErrorKey field in the NSError.userInfo dictionary object will contain more detailed explanation that can be shown to the user.

    See FIRAuthErrors for a list of error codes that are common to all API methods.

    Declaration

    Swift

    func createUser(withEmail email: String, password: String, completion: AuthResultCallback? = nil)

    Objective-C

    - (void)createUserWithEmail:(nonnull NSString *)email
                       password:(nonnull NSString *)password
                     completion:(nullable FIRAuthResultCallback)completion;

    Parameters

    email

    The user’s email address.

    password

    The user’s desired password.

    completion

    Optionally; a block which is invoked when the sign up flow finishes, or is canceled. Invoked asynchronously on the main thread in the future.

  • Resets the password given a code sent to the user outside of the app and a new password for the user.

    Possible error codes:

    • FIRAuthErrorCodeWeakPassword - Indicates an attempt to set a password that is considered too weak.
    • FIRAuthErrorCodeOperationNotAllowed - Indicates the administrator disabled sign in with the specified identity provider.
    • FIRAuthErrorCodeExpiredActionCode - Indicates the OOB code is expired.
    • FIRAuthErrorCodeInvalidActionCode - Indicates the OOB code is invalid.

    See FIRAuthErrors for a list of error codes that are common to all API methods.

    Declaration

    Swift

    func