javascript - passportjs local strategy not getting called -
i using express , passport build rest api backend , seems localstrategy not getting called on request.
the entry point of application looks following:
app.js
var fs = require('fs'); var express = require('express'); var mongoose = require('mongoose'); var passport = require('passport'); var config = require('./config/config'); var morgan = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var app = express(); app.use(morgan('dev')); // log every request console app.use(cookieparser()); // read cookies (needed auth) app.use(bodyparser.urlencoded({extended:true})); app.use(passport.initialize()); //connect mongodb mongoose.connect(config.db, options); //load models (shorten foreach) ... require(__dirname + '/models/' + file)(mongoose); //load passport config require('./config/passport')(mongoose, passport); //load routes require('./config/routes')(app, passport); //start server var server = app.listen(3000, ....
routes.js
... app.post('/auth', function(req, res){ console.log("reached auth endpoint"); console.log(req.body); passport.authenticate('local', { session: false}, function(err, user, info){ console.log("test:"+user); if(err) { console.log("error1"); return next(err)} if(!user){ console.log("error2"); return res.json(401, {error: 'auth error!'}); } console.log("error3"); var token = jwt.encode({ username: user.email }, "hanswurst"); res.json({token: token}); }), function(req, res){ console.log("passport user", req.user); }; });
passport.js
... passport.use(new localstrategy({ usernamefield: 'email', passwordfield: 'password', passreqtocallback: true }, function(email, password, done){ console.log("test"); user.findone({'email': email}, function(err, user){ if(err){ console.log("unknown error"); return done(err); } if(!user){ console.log("no user found"); return done(null, false); } if(!user.validpassword(password)){ console.log("password incorrect"); return done(null, false); } console.log("user found"); return done(null, user); }); } ));
the result on form request is
reached auth endpoint { email: 'test@mail.com', password: 'secret' } post /auth - - ms - -
for me request body looks fine , should enter localstrategy. bit helpless not getting other console output point.
first off, have basic javascript syntax errors in routes.js. part right here (lots of code removed clarity) broken:
passport.authenticate(/*...*/), function(req, res) {/*..*/};
it might have been added console.log
calls in wrong place. clear confusion, passport.authenticate()
not perform authentication right away, return middleware you. use example :
var middleware = passport.authenticate(...); app.post('/auth', middleware);
so fix problem, try invoking middleware returned authenticate right away, this:
app.post('/auth', function(req, res, next) { console.log("reached auth endpoint"); console.log(req.body); passport.authenticate('local', { session: false }, function(err, user, info) { console.log("test:" + user); if (err) { console.log("error1"); return next(err); } if (!user) { console.log("error2"); return res.json(401, { error: 'auth error!' }); } console.log("error3"); var token = jwt.encode({ username: user.email }, "hanswurst"); res.json({ token: token }); })(req, res, next); });
also, have tell require
caches modules. make config/passport.js
aware of mongoose , passport, should not feed them parameters this:
require('./config/passport')(mongoose, passport);
simply require them again inside config/passport.js
so:
// (in config/passport.js) // both of these vars point same thing require'd in app.js var mongoose = require('mongoose'); var passport = require('passport');
Comments
Post a Comment