10

I have a Sails JS application. I am trying to setup authentication using Passport.js authentication layer sails-generate-auth. I have configured my app by following the steps given in their documentation.

But when I lift my sails app, authentication is not working. I am able to access the controllers, even when I am not logged in (It's not redirecting to my login page).

I added a console.log statement in api/policies/passport.js as follows:

module.exports = function (req, res, next) {
  passport.initialize()(req, res, function () {
    passport.session()(req, res, function () {
      res.locals.user = req.user;
      console.log(req.user); // added by me
      next(); 
    });
  });
};

Now, when I access controllers before login or after logout, its printing undefined. But when I am logged in, its printing my user data. Any idea why it is not checking for authentication?

I am using local authentication strategy and I have commented out all others (twitter, facebook...)

2 Answers 2

12

The above answer provides useful information. I want to elaborare on that.

sails-generate-auth, by default doesn't deny access to controllers if the user is not logged in. For that, you can create another policy in api/policies/. For example: create sessionAuth policy as follows:

module.exports = function(req, res, next) {
  if (req.user) {
    return next();
  }

  return res.forbidden('You are not permitted to perform this action.');
};

Instead of showing forbidden page, you can also render login page. For that you need access to AuthController.login. So, add the policies in config/policies as follows:

'*': ['passport', 'sessionAuth'],

'auth': {
  '*': ['passport']
}

This helps to restrict access all the controllers except auth controllers such as login, logout and register, if the user is not logged in.

2
  • I've made the above alterations to config/policies. I am now getting a 401 error for my controllers (for example, GET /user) even AFTER a successful login (via Postman packaged app, which seems to be keeping the relevant cookies). When I run the node debugger, on the sessionAuth policy, I find that req.session exists (with the ID of the current user) but req.session.authenticated was never set, so the authentication fails.
    – ansorensen
    Dec 27, 2014 at 0:26
  • 1
    If you are using sails-generate-auth, currently the module doesn't have support for API authentication. I've given a pull request to add that. But you can use this even-more-passport to know how to integrate API authentication. If you have any questions, raise an issue there. It is maintained by me and I'll surely help you. Dec 27, 2014 at 4:37
6

Passport doesn't have a policy to deny access to a controller. For this, you have to create another policy.

See this link for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.