Search code examples
node.jsexpressloopbackjs

why template not working in reset password loopback


i m just confuse why template not working in reset password function

  user.on('resetPasswordRequest', function(info) {
   var url = 'http://' + config.host + ':' + config.port + '/reset-password';
   var html = 'Click <a href="' + url + '?access_token=' +
    info.accessToken.id + '">here</a> to reset your password';
     user.app.models.Email.send({
          to: info.email,
          from: info.email,
          subject: 'Password reset',
          html: html
    }, function(err) {
       if (err) return console.log('> error sending password reset email');
      console.log('> sending password reset email to:', info.email);
   });
 });

above code is working fine but if i use template intead of html then i got empty template in email for eg like below

    user.app.models.Email.send({
          to: info.email,
          from: info.email,
          subject: 'Password reset',
         template: path.resolve(__dirname, '../../server/views/verify.ejs'),
    }, function(err) {
       if (err) return console.log('> error sending password reset email');
      console.log('> sending password reset email to:', info.email);
   });

if i use this same template in user.verify method it work there then why it not working here
is there any other alternative to provide template in password reset


Solution

  • hello rocky you can try something like this

    var ejs = require("ejs");
    
    var token = info.accessToken.id;
    var pathreset= path.resolve(__dirname, '../../server/views/resetpassword1.ejs');
    
    ejs.renderFile(pathreset, { username: info.user.username, token:token }, function (err, data) {
            if (err) {
                    console.log(err);
             } else {
    
                 var options={
                    to: info.email,
                    from: credentials.user,
                    subject: 'Password reset',
                    html:data
                    }
    
                 User.app.models.Email.send(options, function(err) {
                        if (err) return console.log('> error sending password reset email');
                         console.log('> sending password reset email to:', info.email);
                 });
    

    hope this will help you as well other