Navigation

Working with Multiple User Accounts

Overview

The Stitch Client SDKs allow multiple users to log in at the same time. When multiple users are logged in to an app, one of them is the active user for Stitch requests. You can switch the active user to be any other logged in user at any time; this allows users to quickly switch between logged in accounts on a shared device.

Definitions

  • Logged-in user: a user account that has logged on with one of the Stitch Authentication providers and has not been logged out or removed. A logged-in user may be an active or inactive user.
  • Active user: the one logged-in user account that can currently make calls to Stitch. Any data displayed in the app is data that the active user has permissions to read. See active users for more information.
  • Inactive user: a user account that is not currently the active user but is logged in.
  • Logged-out user: a user account that exists on the app but has logged out. The user is still associated with the device but cannot become the active user until they log in again.

User Account State Diagram

The following diagram explains the state of user accounts in your app when different events occur.

../../_images/multi-user.png

Use Cases

The examples in this section demonstrate how to use methods from the Client SDKs to manage multi-user authentication in a client application.

Log in as two different users

The following code demonstrates how your app can support more than one user logging in.

const stitchAppClient = Stitch.defaultAppClient;
const auth = stitchAppClient.auth;

// Log in as user1
await auth.loginWithCredential(new UserPasswordCredential('user1', 'password'));

// The active user is now user1
const user1 = auth.user;

// Log in as user2
await auth.loginWithCredential(new UserPasswordCredential('user2', 'password'));

// The active user is now user2
const user2 = auth.user;

// See that auth.user has changed upon loginWithCredential()
expect('user1 is not user2', user1.id !== user2.id);

let activeUser = auth.user;

// Verify that active user is user2
expect('active user is user2', activeUser.id === user2.id);

For details on the methods, classes, and interfaces used in this code, see

client = Stitch.getDefaultAppClient();

final StitchUser[] users = new StitchUser[2];

client.getAuth().loginWithCredential(new UserPasswordCredential("user1", "password"))
    .addOnCompleteListener(task -> {
        users[0] = task.getResult();
    });

// The active user is users[0]

client.getAuth().loginWithCredential(new UserPasswordCredential("user2", "password"))
    .addOnCompleteListener(task -> {
        users[1] = task.getResult();
    });

// The active user is users[1], but users[0] is still logged in.

For details on the methods, classes, and interfaces used in this code, see

List users

The following code demonstrates how you can get a collection of all user accounts that are on the device.

const stitchAppClient = Stitch.defaultAppClient;
const auth = stitchAppClient.auth;

// List users
console.log(auth.listUsers());

For details on the methods, classes, and interfaces used in this code, see

client = Stitch.getDefaultAppClient();

// List users
List<StitchUser> loggedInUsers = client.getAuth().listUsers();
for (StitchUser user : loggedInUsers) {
    Log.d("Stitch", "Logged in user: " + user);
}

For details on the methods, classes, and interfaces used in this code, see

Switch users

The following code demonstrates switching which user account is the active user.

const stitchAppClient = Stitch.defaultAppClient;
const auth = stitchAppClient.auth;

// Switch active user to to user1
activeUser = auth.switchToUserWithId(user1.id);

// Verify that active user is now user1
expect('active user is user1', activeUser.id === user1.id);

For details on the methods, classes, and interfaces used in this code, see

// Change the active user
client.getAuth().switchToUserWithId(users[0].getId());

For details on the methods, classes, and interfaces used in this code, see

Remove the active user

The following code shows how to remove the active user from your app.

const stitchAppClient = Stitch.defaultAppClient;
const auth = stitchAppClient.auth;

const user1 = await auth.loginWithCredential(
  new UserPasswordCredential('user1', 'password'));

// user1 is now the active user

// Now remove active user
await auth.removeUser();

For details on the methods, classes, and interfaces used in this code, see

// Remove the active user completely

client.getAuth().removeUser().addOnCompleteListener(task -> {
  if (task.isSuccessful()) {
      Log.d("Stitch", "The active user was removed.");

      StitchUser newActive = client.getAuth().getUser();

      if (newActive == null) Log.d("Stitch", "There is now no active user.");
      else Log.d("Stitch", "The new active user is:" + newActive.getId());
  }
});

For details on the methods, classes, and interfaces used in this code, see

Remove a specific user account

The following code demonstrates removing a user account by passing that account’s id.

const stitchAppClient = Stitch.defaultAppClient;
const {auth} = stitchAppClient;

// List all logged in users
console.log(auth.listUsers());

// Remove user1
await auth.removeUserWithId(user1.id);

For details on the methods, classes, and interfaces used in this code, see

// Remove a specific user
client.getAuth().removeUserWithId(users[1].getId()).addOnCompleteListener(task->{
  if (task.isSuccessful()) {
      Log.d("Stitch", "User" + users[1].getId() + "was removed.");

      StitchUser newActive = client.getAuth().getUser();

      if (newActive == null) Log.d("Stitch", "There is no active user.");
      else Log.d("Stitch", "The new active user is:" + newActive.getId());
  }
});

For details on the methods, classes, and interfaces used in this code, see