Tuesday, 16 May 2017

passport.authenticate is not working

File router.js contains the routers that are used.

In the ionic2, providers are made with the post method by giving uri as http://localhost:8080/api/auth/login which gives 400 bad request error in the debugger tools.

I have found that when I removerequireLoginthen the post is working fine.But I needrequireLogin` for authenticating the local login of passport.

router.js

//require the passport strategy from the folder in the project.
  var passport = require('passport');
  var x = require('./usefile/file');

var requireLogin = passport.authenticate('local',{session: false});

module.exports = function(app){

var auth = express.Router();

app.use('/api/auth', auth);

auth.post('/login', requireLogin, function(req, res){
     x.login(req, res)});
}

passport.js

var passport = require('passport');
var JwtStrategy = require('passport-jwt').Strategy;
var ExtractJwt = require('passport-jwt').ExtractJwt;
var LocalStrategy = require('passport-local').Strategy;
var config = require('./auth');
var User = require('../models/user');

var localOptions = {
    usernameField: 'email',
};

var localLogin = new LocalStrategy(localOptions, function (email, password, done) {
    User.find(
        { email: email }, function (err, user) {
            if (err) {
                return done(err);
            }
            if (!user) {
                return done(null, false, { error: 'Login failed,Please try again.' });
            }
            if (!isMatch) {
                return done(null, false, { error: 'Login failed, Please try again.' });
            }
            user.comparePassword(function(password, isMatch){
                if(err){
                    return done(err);
                }
                if(!isMatch){
                    return done(null, false, {error:'Login failed, Please try again.'});
                }
                return done(null, user);
            });
        });
});
passport.use(localLogin);

Note: My code is not using the requireLogin which is used to authenticate from the passport, So it is showing 400 bad request error.

Please Help.



via Aditya Jain

No comments:

Post a Comment