Looking for an example of user registration on IOS

I’m trying to perform user registration on IOS using Swift. I’d like to find some example code.

Registration works. I’m collecting a JWT from my own backend. Then I’m creating credentials and calling something like this:

RLMApp.login(withCredential: credentials, completion: { })

I am actually able to register a user with the same method, as the login method just creates a new user if the username is not taken.

The main problem I’d like to solve is that I don’t know when a new user has been created. If a user attempts to login and misspells their username, it will just create a new account. If a user tries to signup with a username that’s already taken, they’ll end up either logging in or getting a bad password notification.

Is there a method for synch registration on IOS. Can I get an example of how to call this method?

Also, I’d like to trigger a confirmation email. Am I able to do this if I register with a JWT? If so, can someone point me to an example?

Thanks,
Ryan

1 Like

Ryan,

First of all, good question. Based on your post, I understand that you have a functioning backend that correctly generates a JWT token for MongoDB Realm, so you have already done most of the heavy lifting.

Second, MongoDB Realm supports JWT authentication, but does not actually provide a JWT authentication provider. That is where your backend comes into play. But as a JWT authentication provider, it is responsible for user registration and user password authentication. Once it has determined that a user is valid, it creates a JWT token for that user. When the JWT token is passed on to Realm through the RLMApp.login(…) function, Realm assumes that the user in question is valid. If the actual user does not exist, Realm will go ahead and create the user record for it. The whole idea behind JWT authentication is that creating and validating user credentials is differed to a third party. MongoDB Realm simply provides the hooks for that to happen. Also, the new version of Realm supports meta-data, which can be very useful for packaging additional information during the signup phase.

There are really two things you can do:

  1. Implement the signup and password authentication in your backend before creating the JWT token.
  2. Add an authentication trigger to your MongoDB Realm application to trap the authentication process. And then do some post processing.

In my opinion, the second approach is more of a kludge, and could cause problems down the road.

I wrote a medium article on JWT authentication and meta-data, maybe this will help

Our company Cosync also provides a commercial JWT service for MongoDB Realm at

https://cosync.io

I hope this was useful.

Richard Krueger

2 Likes

Super helpful, thanks richard