Multi-User Applications - Java SDK
On this page
The Realm SDK allows multiple users to be logged in to an app simultaneously on a given device. Realm 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.
Warning
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.
User Account States
When a user first logs in through a Realm SDK on a given device or browser, the 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 Atlas 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 its session revoked.
The following diagram shows how users within a Realm client app transition between states when certain events occur:
Add a New User to the Device
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.
String appID = YOUR_APP_ID; // replace this with your App ID App app = new App(new AppConfiguration.Builder(appID).build()); // Log in as Joe Credentials joeCredentials = Credentials.emailPassword(firstUserEmail, firstUserPassword); app.loginAsync(joeCredentials, it -> { if (it.isSuccess()) { // The active user is now Joe User joe = it.get(); assert joe == app.currentUser(); } else { Log.e("EXAMPLE", "Failed to log in: " + it.getError().getErrorMessage()); } }); // Log in as Emma Credentials emmaCredentials = Credentials.emailPassword(secondUserEmail, secondUserPassword); app.loginAsync(emmaCredentials, it -> { if (it.isSuccess()) { // The active user is now Emma User emma = it.get(); assert emma == app.currentUser(); } else { Log.e("EXAMPLE", "Failed to log in: " + it.getError().getErrorMessage()); } });
val appID: String = YOUR_APP_ID // replace this with your App ID val app = App(AppConfiguration.Builder(appID).build()) // Log in as Joe val joeCredentials = Credentials.emailPassword(firstUserEmail, firstUserPassword) app.loginAsync(joeCredentials) { if (it.isSuccess) { // The active user is now Joe val joe = it.get() assert(joe === app.currentUser()) } else { Log.e("EXAMPLE", "Failed to log in: ${it.error.errorMessage}") } } // Log in as Emma val emmaCredentials = Credentials.emailPassword(secondUserEmail, secondUserPassword) app.loginAsync(emmaCredentials) { if (it.isSuccess) { // The active user is now Emma val emma = it.get() assert(emma === app.currentUser()) } else { Log.e("EXAMPLE", "Failed to log in: ${it.error.errorMessage}") } }
List All Users on the Device
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 client app on a given device regardless of whether they are currently authenticated.
Map<String, User> users = app.allUsers(); for (Map.Entry<String, User> user : users.entrySet()) { Log.v("EXAMPLE", "User: " + user.getKey()); }
val users = app.allUsers() for ((key) in users) { Log.v("EXAMPLE", "User: $key") }
Remove a User from the Device
To remove all information about a user from a device, use user.remove() or user.removeAsync():
app.loginAsync(credentials, it -> { if (it.isSuccess()) { User user = it.get(); user.removeAsync(result -> { if (result.isSuccess()) { Log.v("EXAMPLE", "Successfully removed user from device."); } else { Log.e("EXAMPLE", "Failed to remove user from device."); } }); } else { Log.e("EXAMPLE", "Failed to log in: " + it.getError().getErrorMessage()); } });
app.loginAsync(credentials) { if (it.isSuccess) { val user = it.get() user.removeAsync { result: App.Result<User?> -> if (result.isSuccess) { Log.v("EXAMPLE", "Successfully removed user from device.") } else { Log.e("EXAMPLE", "Failed to remove user from device.") } } } else { Log.e("EXAMPLE", "Failed to log in: ${it.error.errorMessage}") } }
Change the Active User
You can quickly switch an app's active user to another logged in user at any time.
// Joe is already logged in and is the currently active user User joe = app.currentUser(); // Log in as Emma Credentials emmaCredentials = Credentials.emailPassword(secondUserEmail, secondUserPassword); app.loginAsync(emmaCredentials, result -> { if (result.isSuccess()) { // The active user is now Emma User emma = result.get(); assert emma == app.currentUser(); // Switch active user back to Joe app.switchUser(joe); assert joe == app.currentUser(); } else { Log.e("EXAMPLE", "Failed to log in: " + result.getError().getErrorMessage()); } });
// Joe is already logged in and is the currently active user val joe = app.currentUser() // Log in as Emma val emmaCredentials = Credentials.emailPassword( secondUserEmail, secondUserPassword ) app.loginAsync(emmaCredentials) { result -> if (result.isSuccess) { // The active user is now Emma val emma = result.get() assert(emma === app.currentUser()) // Switch active user back to Joe app.switchUser(joe) assert(joe === app.currentUser()) } else { Log.e("EXAMPLE", "Failed to log in: ${result.error.errorMessage}") } }