如何防止 Realm.js 创建不存在的新用户?



我开始使用Realm.js作为我的android应用程序的数据库,但我很难理解为什么如果我使用无效凭据登录,它不会给我错误,而是会创建一个新用户。

我想避免这种情况发生,并给用户一个错误?我是否应该在调用登录函数之前手动检查用户是否存在(在调用之前,之后总是返回true(?

try {
// Reset any previous errors that might have happened
this.setState({ error: undefined });
// Attempt to authenticate towards the server
const credentials = Realm.Sync.Credentials.usernamePassword(nickname, '123'); // 123 password is hard coded,this is only for testing
const user = await Realm.Sync.User.login(SERVER_URL, credentials);
// Hide the modal
this.setState({ isModalVisible: false });
this.onAuthenticated(user);
} catch (error) {
this.setState({ isModalVisible: true, error });
}

只需在这一行添加false,用户不是在登录时创建的,而是在凭据构建时创建的。

const credentials = Realm.Sync.Credentials.usernamePassword(nickname, '123', false); // 123 password is hard coded,this is only for testing

相关内容

最新更新