Email Confirmation Script for User Authentication via Email Address

I’m looking to move across from Firebase to MongoDB (for a number of reasons). But user authentication is a little more “involved”. Based on the Realm UI, I’m required to enter an “email confirmation URL” that should point to a page that contains an email confirmation script.

I’ve tried to look for some tutorials or guides on how to configure this as I’m new to programming. Can anyone point me in the right direction please?

Hi @Anthony_CJ,

The idea is you should set a link pointing to your application url endpoint where you would confirm the received token.

Now the user is sended with an email by Realm system after registration the link he clicks should point to a place where you complete your registration with your sdk language.

Here is the section for user confirmation flow for JS sdk (you should navigate to a similar docs for your sdk):

https://docs.mongodb.com/realm/node/manage-email-password-users#complete-a-user-confirmation

Please let me know if this helps.

Best
Pavel

Thanks. I understand the process, what I don’t know how to do is generate the email configuration script that I will locate at the email confirmation URL. I am building an iOS app so there’s so web app.

Hi @Anthony_CJ

This is more related to language specific redirect, you can also avoid email confirmation or do a function confirmation for your app.

I would recommend looking into our IOS tutorial which shows email signup logic:
https://docs.mongodb.com/realm/tutorial/ios-swift#enable-authentication

Best
Pavel

Hi @Pavel_Duchovny. Thanks but I’ve got that bit sorted. That’s not what I’m asking about. I’m asking about the email confirmation URL and generating an email confirmation script.

I have exactly the same question mark. It seems like you have to set up you own website with scripts to handle the confirmation link. Apparently in previous versions (Realm Cloud or Stitch) they provided this service for the clients but not anymore. I’m trying to develop a desktop app with Realm, so I don’t have a webserver, which could do that job. Thus this feature is not really useable for me.
Using custom functions to might be the answer, but because of a lack of examples I don’t see how I still can validate that users provided a valid email (for password resets) .

Hi @Anthony_CJ,

I think its definitely possible to create a link which redirect to your ios app instead of a web page:

https://stackoverflow.com/questions/15621641/ios-url-redirect-from-mail-to-app

This is a bit out of scope for realm per say.

The code to confirm is:
https://docs.mongodb.com/realm/ios/manage-email-password-users#confirm-a-new-user-s-email-address

As I said if you don’t want to use an email you can send the user SMS via twilio or email with a code and have the confirmation function input this code from email.

Thanks
Pavel

Hello, I was in the same boat as you and here are the steps I did to add email confirmation to my application. I am going to assume you have some knowledge with the MongoDB Realm website.

Steps

Turn On Email/Password Authentication Provider

Click on the “Email/Password” provider.

Ensure it is ON.

Add Registration Code To Application

Insert this code where you make the user sign up.

Create Confirmation Script

This is the super simple code that confirms your user when clicks on the “confirm email” link that they get in their confirmation email.

<html>
<head>
<script src="https://unpkg.com/realm-web@1.2.0/dist/bundle.iife.js"></script>
<script>
const APP_ID = "< YOUR APP_ID >";
const app = new Realm.App({ id: APP_ID});
//Grab Tokens
const params = new URLSearchParams(window.location.search);
const token = params.get('token');
const tokenId = params.get('tokenId');
//Confirm client
app.emailPasswordAuth
    .confirmUser(token, tokenId)
    .then(() => displayResult('success'))
    .catch(err => displayResult('error', err))
//Display Message depending on result
function displayResult(result, err) {
    const message = document.getElementById("message");
    if (result === "success") {
        message.innerText = "Your E-mail address has been verified.\n\n You may close this page. Thank you.";
    }
   else if (result === "error") {
       message.innerText = "Unable to register this user. Please try again to register." + err;
   }
}
</script>

<title>E-mail Confirmation</title>
<style>
    h1 {text-align: center;}
    h3 {text-align: center;}
    h5 {text-align: center; background-color: whitesmoke;}
    p {text-align: center;}
    div {text-align: center;}
</style>
</head>
<body>
    <h1>My Apps Name</h1>
    <h3>Thanks for choosing our app!</h1>
    <h5 id = "message"></h5>
</body>
</html>

This line here “” allows us to use Realm as a global variable, and the rest is pretty self explanitory.
** Ensure to put your own APP_ID **

Hosting the script

Now you have to host the script, this is super simple. Go to your Realm Apps home page and on the left hand side you should see a tab called “Hosting”, click on it.

Then put the code from the previous step into a file and upload that file by clicking “Upload Files”, and selecting that file.

Enter Script URL into Email/Password Authentication Provider

This is the last step to this process, and the easiest, simple copy the link of the script,


and add it to the email.

Done!

Thats all you need to do to have email confirmation with your iOS application using MongoDB Realm and Swift. The process for password reseting is very identical as well.