Search code examples
react-nativerealmrealm-mobile-platformrealm-js

How prevent Realm.js from creatig new users if they don't exist?


I'm starting to use Realm.js as the database for my android apps but I have a problem understanding why if I log with invalid credentials it doesn't give me an error but instead it creates a new user.

I'd like to avoid this from happening and give the user an error? Am I supposed to check manually if the user exists before calling the login function (before doing it after will always return 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 });
}

Solution

  • Just add false at this line, the user is not created on login, but on credentials building.

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