Get verification token on user.verify.

489 views
Skip to first unread message

Salvatore Calla'

unread,
Mar 20, 2016, 12:36:58 PM3/20/16
to LoopbackJS
Hi guys!
I'm using a template ejs file to send a verification email after user registration.
How can i access the verification url generated by loopback and put it inside my template?

What i'm trying to do is to obtain a message like this

Hi newusername!
Click here to confirm your email address.
......

Best regards.

Salvo

Dinesh Ramasamy

unread,
Mar 21, 2016, 12:45:41 PM3/21/16
to LoopbackJS
Hi Salvo,

Pass it in options something like,

user.afterRemote("create", function(context, user, next) {
    var options = {
      type: "email",
      to: user.email,
      template: path.resolve(__dirname, emailTemplatePath),
      name: user.name,
    };

    user.verify(options, function(err, response, next) {
      //
    });
  });

Salvatore Calla'

unread,
Mar 25, 2016, 2:50:07 AM3/25/16
to LoopbackJS
Hi Dinesh,
thanks for the response.
The problem is tha i can't get the verification url and put it inside my template.
I want to do something like this

  user.afterRemote("create", function(context, user, next) {
    var options = {
      type: "email",
      to: user.email,
      template: path.resolve(__dirname, emailTemplatePath),
      name: user.name,
      verificationUrl: < Url computed by loopback >
    };

    user.verify(options, function(err, response, next) {
      //
    });
  });
  

Dinesh Ramasamy

unread,
Mar 25, 2016, 5:14:21 AM3/25/16
to loopb...@googlegroups.com
Hi, 

Just do this, It should work.

  user.afterRemote("create", function(context, user, next) {
    var options = {
      type: "email",
      to: user.email,
      template: path.resolve(__dirname, emailTemplatePath),
      name: user.name,
      verificationUrl: "{href}"
    };

    user.verify(options, function(err, response, next) {
      //
    });
  });

--
You received this message because you are subscribed to a topic in the Google Groups "LoopbackJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/loopbackjs/A9gT2ZLuges/unsubscribe.
To unsubscribe from this group and all its topics, send an email to loopbackjs+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/63d2ebb8-e148-4242-9ac8-f650196ffcff%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Salvatore Calla'

unread,
Mar 25, 2016, 11:58:57 AM3/25/16
to LoopbackJS
Can you clarify what is {href}?

Dinesh Ramasamy

unread,
Mar 25, 2016, 12:46:24 PM3/25/16
to loopb...@googlegroups.com

rites...@anthropower.in

unread,
Apr 5, 2016, 3:17:15 AM4/5/16
to LoopbackJS
Please see the file node_modules/loopback/common/models/user.js > User.prototype.verify > sendEmail at around line 403. 
var template = loopback.template(options.template);
options
.html = template(options);

Email.send(options, function(err, email) {
 
if (err) {
 fn
(err);
 
} else {
 fn
(null, {email: email, token: user.verificationToken, uid: user.id});
 
}
});

template(options) method compiles the ejs template by providing the entire options object and then compiled template is stored on options.html property for email.

So you can use any of options's property. Link https://github.com/strongloop/loopback-example-user-management/blob/master/common/models/user.js#L9-L17 shows how entire user object is passed and you can use any of the property defined on user object. For example, in ejs template like this for user's name
<%= user.name %>

options.verifyHref is created inside the user.js > User.prototype.verify method. So you can use <%= verifyHref %>. Please see if this works.

----------------------
You might also need this
The verification link that gets generated is one time use only. Clicking again will show error because verification token expires. I do not consider it as a good experience for the users. In case of error, showing a screen which shows message like "Your account has already been verified" or "Verification link has been used" or whatever would be desired. Please let me know in case you'll need to override this error. 

Marianny Salcedo

unread,
Jul 1, 2016, 7:57:02 PM7/1/16
to loopb...@googlegroups.com
I am trying to use this approach as well. So far, I get the verification url and when I click it, the server tells me that  a token is required.

module.exports = function(UserW) {
UserW.afterRemote('create', function(context, UserW, next) {
    console.log('> UserW.afterRemote triggered');

    var options = {
      type: 'email',
      to: UserW.email,
      from: 'nor...@loopback.com',
      subject: 'Gracias por registrarte.',
      // template: path.resolve(__dirname, '../../server/views/verify.ejs'),
      redirect: 'http://localhost:9000/#/login',
      user: UserW,
      // text: 'Gracias por registrarte. Por favor, sigue el siguiente link para completar tu registro.\n\t{href}',
      href: "{href}"
    };

    UserW.verify(options, function(err, response) {
      if (err) return next(err);

      console.log('> verification email sent:', response);
      return response;
      context.res.render('response', {
        title: 'Registro exitoso!',
        content: 'Por favor, revisa tu correo y verificate antes de entrar al sistema. ',
        redirectTo: 'http://localhost:9000/#/login',
        redirectToLinkText: 'Log in'
      });
    });
  });
};


Akram Shehadi

unread,
Jul 2, 2016, 9:18:39 PM7/2/16
to LoopbackJS
What I do to get the link is the following.

So I guess in your case you should be able to do UserW.id.

User.afterRemote('create', function(context, user, next) {

    var verifyLink = 'https://' +
                        hostAddress
+
                       
':' +
                        portNumber
+
                        restApiRoot
+
                       
'/Users/confirm' +
                       
'?uid=' +
                        user
.id +
                       
'&redirect=https://' + hostAddress + '/verified?user_id='+user.id;


    logger
.verbose("Link: " + verifyLink);


   
var options = {
      type
: 'email',

      mailer
: Email,
      to
: user.email,
     
from: serviceEmail,
      subject
: 'User registration confirmation',
     
template: path.resolve(__dirname, '../views/verify.ejs'),
     
//text: 'Thanks for registering! Click here for verification: \n\t{href}',
      user
: user,
      verifyHref
: verifyLink,
     
//protocol: 'https',
     
//host : hostAddress,
     
//port : portNumber,
      host
: serviceEmailHost,
      port
: serviceEmailPort
   
};

    user
.verify(options, function(err, response) {
     
if (err) {
       
next(err);
       
return;
     
}
      logger
.verbose("Account verification email sent to " + options.to);
     
next();
     
return;
   
});
   
//more code
   
//...
   
//...
 
});
}
Reply all
Reply to author
Forward
0 new messages