Docs Menu
Docs Home
/ /
Atlas Device SDKs
/ /

Multi-User Applications - Swift SDK

On this page

  • User Account States
  • Add a New User to the Device
  • List All Users on the Device
  • Change the Active User
  • Remove a User from the Device

The Realm Swift SDK allows multiple users to be logged in to an app simultaneously on a given device. Client applications run in the context of a single active user even if multiple users are logged in simultaneously. You can quickly switch between authenticated users without requiring them to log in again.

Important

Any logged in user may become the active user without re-authenticating. Depending on your app, this may be a security vulnerability. For example, a user on a shared device may switch to a coworker's logged in account without providing their credentials or requiring their explicit permission. If your application requires stricter authentication, avoid switching between users and prefer to explicitly log the active user out before authenticating another user.

When a user first logs in through Atlas App Services on a given device or browser, the Realm SDK saves the user's information and keeps track of the user's state on the device. The user's data remains on the device, even if they log out, unless you actively remove the user.

The following states describe an on-device user at any given time:

  • Authenticated: any user that has logged in on the device and has not logged out or had its session revoked.

    • Active: a single authenticated user that is currently using the app on a given device. The SDK associates this user with outgoing requests and App Services evaluates data access permissions and runs functions in this user's context. See active user for more information.

    • Inactive: all authenticated users that are not the current active user. You can switch the active user to a currently inactive user at any time.

  • Logged Out: any user that authenticated on the device but has since logged out or had their session revoked.

The following diagram shows how users within a client app transition between states when certain events occur:

A diagram the outlines the different states a user can be in: logged out, logged in and active, & logged in and inactive.

The Realm SDK automatically adds users to a device when they log in for the first time on that device. When a user logs in, they immediately become the application's active user.

let app = App(id: YOUR_APP_SERVICES_APP_ID)
let joeCredentials = Credentials.emailPassword(email: "joe@example.com", password: "passw0rd")
app.login(credentials: joeCredentials) { (result) in
switch result {
case .failure(let error):
print("Login failed: \(error.localizedDescription)")
case .success(let joe):
// The active user is now Joe
assert(joe == app.currentUser)
}
}
let emmaCredentials = Credentials.emailPassword(email: "emma@example.com", password: "pa55word")
app.login(credentials: emmaCredentials) { (result) in
switch result {
case .failure(let error):
print("Login failed: \(error.localizedDescription)")
case .success(let emma):
// The active user is now Joe
assert(emma == app.currentUser)
}
}

You can access a list of all user accounts that are stored on the device. This list includes all users that have logged in to the app on a given device regardless of whether they are currently authenticated.

let app = App(id: YOUR_APP_SERVICES_APP_ID)
let users = app.allUsers
users.forEach({ (key, user) in
print("User: \(key) \(user)")
})

You can change an app's active user to another logged in user at any time with the following code:

let app = App(id: YOUR_APP_SERVICES_APP_ID)
// ... log in ...
// Get another user on the device, for example with `app.allUsers`
let secondUser: User = getSomeOtherUser()
XCTAssertNotEqual(app.currentUser, secondUser)
// assert(app.currentUser != secondUser)
// Switch to another user
// app.switch(to: secondUser)
// The switch-to user becomes the app.currentUser
// XCTAssertEqual(app.currentUser, secondUser)
// assert(app.currentUser == secondUser)

You can remove all information about a user from the device and automatically log the user out.

app.currentUser?.logOut { (error) in
// user is logged out or there was an error
}

Back

Manage Email/Password Users