多用户应用程序 — Java SDK
Atlas Device SDK 已弃用。 有关详细信息,请参阅弃用页面。
Realm 软件开发工具包(Realm SDK)允许多个用户在给定设备上同时登录应用。 即使多个用户同时登录, Realm 客户端应用程序也会在单个活动用户的上下文中运行。 您可以在经过身份验证的用户之间快速切换,而无需他们再次登录。
警告
任何登录用户都可以成为活动用户,而无需重新进行身份验证。 这可能是一个安全漏洞,具体取决于您的应用程序。 例如,共享设备上的用户可以切换到同事的登录帐户,而无需提供他们的凭证或征求他们的明确许可。 如果您的应用程序需要更严格的身份验证,请避免在用户之间切换,并且最好在对其他用户进行身份验证之前显式注销活动用户。
用户账户状态
当用户首次通过给定设备或浏览器上的 Realm SDK 登录时,SDK 会保存用户的信息并跟踪用户在设备上的状态。 即使用户注销,其数据仍会保留在设备上,除非您主动删除用户。
以下状态描述了设备上的用户在任何给定时间的状态:
已通过身份验证:已登录设备且未注销或撤销会话的任何用户。
已注销:在设备上进行身份验证但已注销或已撤销会话的任何用户。
下图展示了在特定事件发生时,Realm 客户端应用程序中的用户如何在各个状态之间进行转换:
向设备添加新用户
当用户首次登录设备时,Realm SDK 会自动将用户添加到该设备。 用户登录后,将立即成为应用程序的活跃用户。
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}") } }
列出设备上的所有用户
您可以访问权限设备上存储的所有用户帐户的列表。 此列表包括在给定设备上登录过客户端应用的所有用户,无论他们当前是否经过身份验证。
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") }
从设备上删除用户
要从设备中删除有关用户的所有信息,请使用user.remove()或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}") } }
更改活动用户
您可以随时将应用程序的活跃用户快速切换成其他登录用户。
// 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}") } }