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

时间:2018-11-02 14:51:37

标签: react-native realm realm-mobile-platform 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 });
}

1 个答案:

答案 0 :(得分:0)

只需在此行添加false,就不会在登录时创建用户,而是在建立凭据时创建用户。

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