Navigation

Anonymous Authentication

Overview

The Anonymous authentication provider allows users to log in to your application without providing credentials. Each time someone authenticates anonymously, the provider generates a new anonymous user object for that session. Anonymous user objects have a unique ID value but no other metadata fields or configuration options.

Anonymous authentication should not be used if you need to persist user data and identities across multiple sessions. Once a particular anonymous user logs out, they will not be able to re-authenticate as the same user.

Potential use cases for anonymous authentication include:

  • Authenticating the readers of a blog or news service.
  • Allowing end users to try the features of an application before registering for an account.
  • Simplifying the creation of users while developing and testing the client application.

You can combine an anonymous user with a user from another authentication provider by linking the user object for each provider. This is helpful for transitioning to a non-anonymous authentication provider or to “upgrade” casual users to more permanent accounts. See Linking User Accounts for more information.

Configuration

You can enable the Anonymous authentication provider from the Stitch UI by selecting Anonymous from the Users > Providers page.

You can enable the Anonymous authentication provider with stitch-cli by importing an application directory that contains a configuration file for the provider.

The configuration file must be named anon-user.json and stored in the /auth_providers directory. Configuration files for the Anonymous authentication provider have the following form:

/auth_providers/anon-user.json
{
  "name": "anon-user",
  "type": "anon-user",
  "disabled": <boolean>,
}

Note

The anonymous authentication provider does not have any provider-specific configuration options.

Usage

Authenticate a User

To authenticate a user anonymously, call StitchAppClient.auth.loginWithCredential() with an instance of AnonymousCredential, returning a Promise instance. If the authentication succeeds, the Promise resolves with an instance of StitchUser:

Stitch.defaultAppClient.auth.loginWithCredential(new AnonymousCredential()).then(user => {
   console.log(`Logged in as anonymous user with id: ${user.id}`);
}).catch(console.error);

To authenticate a user anonymously, instantiate an AnonymousCredential and pass it as the argument to StitchAuth.loginWithCredential().

Stitch.getDefaultAppClient().getAuth().loginWithCredential(new AnonymousCredential()).addOnCompleteListener(new OnCompleteListener<StitchUser>() {
    @Override
    public void onComplete(@NonNull final Task<StitchUser> task) {
        if (task.isSuccessful()) {
            Log.d("stitch", "logged in anonymously");
        } else {
            Log.e("stitch", "failed to log in anonymously", task.getException());
        }
}

To authenticate a user anonymously, call StitchAuth.login() with an instance of AnonymousCredential:

Stitch.defaultAppClient!.auth.login(withCredential: AnonymousCredential.init()) { result in
    switch result {
    case .success:
        print("logged in anonymously")
    case .failure(let error):
        print("failed to log in anonymously: \(error)")
    }
}