Manu Chawla
Manu Chawla

Reputation: 327

isAuthenticated return function false

I have implemented the passport.js while login i put my passport.authenticated() function in middleware.

app.post('/api/v1/login',
        function (req, res, next) {
            passport.authenticate('local-login', function (err, user, info) {
                if (user == false) {
                    return res.json(ApiException.newNotAllowedError(api_errors.invalid_auth_credentials.error_code, null).addDetails(api_errors.invalid_auth_credentials.description));
                }
                else {
                    next();
                }
            })(req, res, next);
        }, controllerIndex.auth.login);

and login is successfully.

when i authenticate other request by using isAuthenticate() function it return false.

if i remove the middle-ware function from passport.authenticated then other request is returning true. but i need middle ware function because to return custom response while user is not authenticated. please any one help me what i have implement wrong.

Upvotes: 1

Views: 102

Answers (1)

owais
owais

Reputation: 4922

you have to set cookie or make sure user is login using req.login

app.post('/api/v1/login',
  function (req, res, next) {
   passport.authenticate('local-login', function (err, user, info) {
     if (user == false) {
        return res.json(ApiException.newNotAllowedError(api_errors.invalid_auth_credentials.error_code, null).addDetails(api_errors.invalid_auth_credentials.description));
     }else{

   req.logIn(user, function(err) {
      if (err) { return next(err); }
      next();
    });

   }
})(req, res, next);
}, controllerIndex.auth.login);

Upvotes: 1

Related Questions