0

I have been trying to figure out a way of passing a link in req.flash() in my js file, without actually doing that in html template, because am using the alert message as a partial in other html pages, I wish to pass in resend verification link, is there a way around this, or do i really have to create another req.flash for verify

app.use(function(req,res,next){
    res.locals.error = req.flash("error");
    res.locals.success = req.flash("success")
    next();
})

router.get("/verification", function(req,res){
    verification coding stuff here
})

the verification route is the link that i wish to pass in the req.flash('error')

router.get('/verify/:token', function(req,res){
  User.findOne({ verificationToken: req.params.token, resetPasswordExpires: { $gt: Date.now()}}, function(err, user){
      if(err){
          console.log(err);
          //here is where i wish to pass in the link for verification link//
          req.flash('error', 'Mail verify token is invalid or has expired' + resend link here + '.')
          return res.redirect('/forgot')
      } else {
          user.active = "true"
          user.save()
          console.log(user.active);
          req.flash('success', 'Your mail have been verified successfully')
          res.redirect('/login');
      }
  })
})

<% if(error && error.length > 0) { %>
    <div class="alert alert-danger alert-dismissible deposit-alert" role="alert">
        <div class="container">
            <%= error %>
        </div>
    </div>
<% } %>
Fillipo Sniper
  • 419
  • 2
  • 11
  • 28

1 Answers1

3

The following should work for you.

Controller::

router.get('/verify/:token', function(req,res){
  User.findOne({ verificationToken: req.params.token, resetPasswordExpires: { $gt: Date.now()}}, function(err, user){
      if(err){
          console.log(err);
          //here is where i wish to pass in the link for verification link//
          var link  = '/verification/';
          var message =  'Mail verify token is invalid or has expired, <a href="'+ link +'"> Click here to resend the link</a>.'
          req.flash('error',message)
          return res.redirect('/forgot')
      } else {
          user.active = "true"
          user.save()
          console.log(user.active);
          req.flash('success', 'Your mail have been verified successfully')
          res.redirect('/login');
      }
  })
})

Template Instead of using <%= message %> use this. <%- message %>

<% if(error && error.length > 0) { %>
    <div class="alert alert-danger alert-dismissible deposit-alert" role="alert">
        <div class="container">
          <%- message %> 
        </div>
    </div>
<% } %>
Farhan Tahir
  • 2,096
  • 1
  • 14
  • 27
  • thanks for replying, i have tried your solution but it just prints out the same way you wrote it e.g click and the link is not working. the req.headers.. is printed out correct with my domain. any other way :) @FarhanTahir – Fillipo Sniper Mar 30 '18 at 12:51
  • @FillipoSniper Can you share the screen shot how it appears. and what happens when you click on the link ? mean any error on console or anything ? – Farhan Tahir Mar 30 '18 at 12:54
  • It doesn't show any error, it just flashes it the same way with other text as if it is a normal text @FarhanTahir – Fillipo Sniper Mar 30 '18 at 13:00
  • Share the code which you are using to display the flash message. – Farhan Tahir Mar 30 '18 at 13:02
  • I have just updated my code as you requested, you might wanna check it out @FarhanTahir – Fillipo Sniper Mar 30 '18 at 13:09
  • yeah it works out but then i think you should edit your code a bit, in the req.headers.host i don't think if it is needed cause it's just a duplicate of the domain as it redirects to page not found. things work out now :) @FarhanTahir – Fillipo Sniper Mar 30 '18 at 13:28
  • @FillipoSniper I'm glad I was able to help, Hey check my answer now removed the req.headers.host. – Farhan Tahir Mar 30 '18 at 13:29