Authenticate with Firebase Anonymously on iOS

You can use Firebase Authentication to create and use temporary anonymous accounts to authenticate with Firebase. These temporary anonymous accounts can be used to allow users who haven't yet signed up to your app to to work with data protected by security rules. If an anonymous user decides to sign up to your app, you can link their sign-in credentials to the anonymous account so that they can continue to work with their protected data in future sessions.

Before you begin

  1. Add Firebase to your iOS project.
  2. Include the following pods in your Podfile:
    pod 'Firebase/Auth'
    
  3. If you haven't yet connected your app to your Firebase project, do so from the Firebase console.
  4. Enable anonymous auth:
    1. In the Firebase console, open the Auth section.
    2. On the Sign-in Methods page, enable the Anonymous sign-in method.

Authenticate with Firebase anonymously

When a signed-out user uses an app feature that requires authentication with Firebase, sign in the user anonymously by completing the following steps:

  1. Import the Firebase module in your UIApplicationDelegate subclass:

    Objective-C

    @import Firebase;
    

    Swift

    import Firebase
    
  2. Configure a FIRApp shared instance, typically in your application's application:didFinishLaunchingWithOptions: method:

    Objective-C

    // Use Firebase library to configure APIs
    [FIRApp configure];
    

    Swift

    // Use Firebase library to configure APIs
    FIRApp.configure()
    
  3. Call the signInAnonymouslyWithCompletion: method:

    Objective-C

    [[FIRAuth auth]
     signInAnonymouslyWithCompletion:^(FIRUser *_Nullable user, NSError *_Nullable error) {
       // ...
     }];
    

    Swift

    FIRAuth.auth()?.signInAnonymously() { (user, error) in
      // ...
    }
    
  4. If the signInAnonymouslyWithCompletion: method completes without error, you can get the anonymous user's account data from the FIRUser object:

    Objective-C

    BOOL isAnonymous = user.anonymous;  // YES
    NSString *uid = user.uid;
    

    Swift

    let isAnonymous = user!.isAnonymous  // true
    let uid = user!.uid
    

Convert an anonymous account to a permanent account

When an anonymous user signs up to your app, you might want to allow them to continue their work with their new account—for example, you might want to make the items the user added to their shopping cart before they signed up available in their new account's shopping cart. To do so, complete the following steps:

  1. When the user signs up, complete the sign-in flow for the user's authentication provider up to, but not including, calling one of the FIRAuth.signInWith methods. For example, get the user's Google ID token, Facebook access token, or email address and password.
  2. Get an FIRAuthCredential for the new authentication provider:

    Google Sign-In
    Objective-C
    GIDAuthentication *authentication = user.authentication;
    FIRAuthCredential *credential =
    [FIRGoogleAuthProvider credentialWithIDToken:authentication.idToken
                                     accessToken:authentication.accessToken];
    
    Swift
    guard let authentication = user.authentication else { return }
    let credential = FIRGoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                      accessToken: authentication.accessToken)
    
    Facebook Login
    Objective-C
    FIRAuthCredential *credential = [FIRFacebookAuthProvider
                                     credentialWithAccessToken:[FBSDKAccessToken currentAccessToken]
                                     .tokenString];
    
    Swift
    let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
    
    Email-password sign-in
    Objective-C
    FIRAuthCredential *credential =
        [FIREmailPasswordAuthProvider credentialWithEmail:email
                                                 password:password];
    
    Swift
    let credential = FIREmailPasswordAuthProvider.credential(withEmail: email, password: password)
    
  3. Pass the FIRAuthCredential object to the sign-in user's linkWithCredential:completion: method:

    Objective-C
        [[FIRAuth auth]
     .currentUser linkWithCredential:credential
     completion:^(FIRUser *_Nullable user, NSError *_Nullable error) {
       // ...
     }];
    
    Swift
        user.link(with: credential) { (user, error) in
      // ...
    }
    }
    

If the call to linkWithCredential:completion: succeeds, the user's new account can access the anonymous account's Firebase data.

Next steps

Now that users can authenticate with Firebase, you can control their access to data in your Firebase database using Firebase rules.

Send feedback about...

Need help? Visit our support page.