niksmac
niksmac

Reputation: 2782

Use Authentication function in express route

I have my controller like this

//Get the Helpers
var authHelper = require('authHelper');

//Route
router.get('/manager', authHelper.checkPerm(req, res, next), function(req, res) {

});

my authHelper.js

exports.checkPerm = function(req, res, next){
  if (req.user) {
    next();
  } else {
    res.redirect('/sign-in');
  }
}

This is the error i am getting

router.get('/manager', authHelper.checkPerm(req, res, next), function(req, res ^ ReferenceError: req is not defined

Upvotes: 0

Views: 148

Answers (1)

Rahil Wazir
Rahil Wazir

Reputation: 10142

You are executing the method checkPerm when passing to the route parameter. Remove the parenthesis along with parameters (req, res, next)

Should be like this:

router.get('/manager', authHelper.checkPerm, function(req, res) ...

Upvotes: 1

Related Questions