Thursday 20 April 2017

Authentication on node js and express js

I have built login page authentication using passport local strategy. Part of the code in users.js goes here which is successful..

router.post('/login',
      passport.authenticate('local', {successRedirect:'/', failureRedirect:'/users/login',failureFlash: true}),
      function(req, res) {
        res.redirect('/');
      });

But I want to authenticate login using my company credentials through ldap for which I have own module called 'bluepages' which I have replaced the above block as below.

router.post('/login',function(req,res){
    //console.log(req.body);
    res.redirect('/');
    bluepages.authenticate(req.body.username,req.body.password,function(err,verified){
    if(err) console.log(err);
    else {
        if(verified)
            return res.redirect('/');
            //console.log('Success');
        else
            return res.redirect('/users/login');
            //console.log('Invalid Credentials');

    }
});
});

I get 'success' log when the credentials are correct and 'Invalid Credentials' log when credentials are incorrect. This is working perfect. But my requirement is when res.redirect is not directing to the required view. It remains in login page itself even though credentials are correct. I get the following error which I am not able to solve it. Can you please help me on how to direct the views.

_http_outgoing.js:344
    throw new Error('Can\'t set headers after they are sent.');
    ^

Error: Can't set headers after they are sent.



via sandeep sampathu

No comments:

Post a Comment