Resend confirmation email rule/button

Hi all,

I found the Auth0 Management API v2 page but don’t really know how to target it, I am trying to make a rule to send a verification email to users post-login if the user email is not confirmed yet. I preferable wanted to do this in the angular code to have a button for users to request it to be sent, but couldn’t find anything to help me set it up as I don’t know how to call the api.

My rule code looks like this:

function (user, context, callback) {
if (!user.email_verified) {
  request.post({
  url: 'https://companyname.eu.auth0.com/api/v2/jobs/verification-email',
    json: {
        user: user,
        context: context,
       secretToken: configuration.MY_SECRET_TOKEN,
     },
    timeout: 1000
 }, function(err, response, body){
if (err)
  return callback(new Error(err));

  return callback(null, user, context);
 });
} else {
return callback(null, user, context);
 }

}

1 Like

Hi @nkr,

Using the management API library for node will make life a lot easier.

Check out the best practices for using the library in rules:

Here is the doc for that library, specifically the email verification function.
http://auth0.github.io/node-auth0/module-management.ManagementClient.html#sendEmailVerification

Let me know if this answers your question!

Hi @dan.woda,

Thank you for the response :slight_smile:

I am not running node with this setup.
I tried to use your reply as a guideline but still having issues.

I got this:

this.auth0Manage = new auth0.Management({
  domain: AUTH_CONFIG.domain,
  token: this._accessToken
});

and later the method I call:

public sendConfirmationEmail(){
var params = {
  user_id: this.sessionService.getCurrentId()
};
this.auth0Manage.sendEmailVerification(params, function(err) {
  if (err) {
    // Handle error.
    console.log(err);
  }
  console.log("email sent");
});

}

with this I get error “this.auth0Manage.sendEmailVerification is not a function”… If I try to use ManagementClient instead of Management, it will only return undefined - Management gives a clear connection to API v2 though.

1 Like

Hi @nkr,

I’m sorry, I misunderstood your first post.

What you’re are trying to do is not recommended due to the nature of single page applications. See more on the topic here.

If you don’t want to use Auth0 Rules to send the email programmatically, you will want to use your backend to access the Management API.

I would be happy to answer any more questions you have on the topic.

Good luck :smiley:!

Hi @dan.woda

I’m trying to set up the rule now as following, but getting failed logins with error “must specify user ID”:

function (user, context, callback) {
  var ManagementClient = require('auth0@2.9.1').ManagementClient;
  var management = new ManagementClient({
     token: auth0.accessToken,
    domain: auth0.domain
  });
  var params = {
       user_id: '{USER_ID}'
  };

   management.sendEmailVerification(function (err) {
       if (err) {
             // Handle error.
       }
   });
}

In the log however, I can see I do have a ’ “user_id”: “auth0|275”, ’ associated with the user login attempt. Do I need to pull out the user id to the params, from the user in the rule/funciton param?

It looks like the sendEmailVerification function is missing params.

Try this:

  var ManagementClient = require('auth0@2.9.1').ManagementClient;
  var management = new ManagementClient({
     token: auth0.accessToken,
     domain: auth0.domain
  });
  var params = {
       user_id: '{USER_ID}'
  };

   management.sendEmailVerification(params, function (err) {
       if (err) {
             // Handle error.
       }
   });
}

Hi @dan.woda

Thank you ! It works now :slight_smile: Code looks like following now in the rule:

function (user, context, callback) {
 var ManagementClient = require('auth0@2.9.1').ManagementClient;
 var management = new ManagementClient({
    token: auth0.accessToken,
   domain: auth0.domain
  });

  var params = {
       user_id: user.user_id
   };
  console.log("1", user);
  console.log("2", params.user_id);
  management.sendEmailVerification(params, function (err) {
       if (err) {
           // Handle error.
          console.log(err);
       }

   console.log("email sent?");
   callback(null, user, context);
  });
}

Just a heads up, the example for “sendVerificationEmail” API you sent previous was missing the params in the method call - which is why it was missing in my previous post :slight_smile:

A last question, is it possible to make a call to my Azure SQL DB when the send verification was successfully sent? Got a variable on my user I want to update in database at the same time. Edit: Found my answer to the db call stuff by going to ‘connections-> database → verify’ :slight_smile:

Best regards,
nkr

Hi @dan.woda

It works fine, but email_verified doesn’t get assigned to users post-email verify so they will always get sent a new confirmation email when they sign in, how do I proceed from here to make it work?

Kind regards,
nkr

Glad this is working for you. I noticed the error in the doc as well, working on getting that fixed :wrench: .

In regards to your most recent update- It looks like the users are getting a verification email with this rule regardless of whether or not there are verified. Typically you would want to add a conditional that performs the callback if the user is already verified.

if (user.email_verified) {
    return callback(null, user, context);
  } else {
    //Your email verification logic 
  }

Let me know if that fixes it.

Hi @dan.woda,

Thank you for the quick response :slight_smile:
I tried that already, but none of my users have that, and I do not have permissions to change user metadata either… Could this be because we are using a free account with no subscriptions? We’ve basically only been running tests so far to see if Auth0 fits our needs, and I forgot we did not subscribe for a plan yet - maybe the lack of variable on the user could be because of this?

Your subscription level shouldn’t matter for this. Go to the users tab in your dashboard, select a user, then select the ‘Raw JSON’ tab. Do you see email_verified at the top?

No, checked several and none of them have a “email_verified” in the Raw JSON format tab :confused:

It sounds like you may be encountering the same problem as this user:

1 Like

Thank you very much for the help @dan.woda, made it work with the topic you sent :slight_smile:
Have a great weekend, cheers.

Solution

Rule function code:

function (user, context, callback) {
   user.user_metadata = user.user_metadata || {};
   if (user.email_verified || user.user_metadata.verification_email_sent) {
     return callback(null, user, context);
   }

   var ManagementClient = require('auth0@2.9.1').ManagementClient;
   var management = new ManagementClient({
     token: auth0.accessToken,
     domain: auth0.domain
   });

   var params = {
       user_id: user.user_id
   };
   console.log("user object: ", user);

    management.sendEmailVerification(params, function (err) {
       if (err) {
         // Handle error.
         console.log(err);
       }
       console.log("email sent?");

       callback(null, user, context);
    });
 }

To assign email_verified on users, in Connections > Database > Login template:

assign to callback

callback(null, {
  user_id: rows[0][0].value,
  email: rows[0][1].value,
  email_verified: rows[0][3].value
});

Glad to hear it! You too.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.