Passport authentication error in the base example

I am trying to break this passport.js example into its most elementary elements. I continue to receive message 401 (unauthorized) and cannot understand why. Any help would be greatly appreciated.

Thanks!

Node.js file:

var http = require('http'), express = require('express'), passport = require('passport'), LocalStrategy = require('passport-local').Strategy, flash = require('connect-flash'); var port = process.env.PORT || 8080; passport.serializeUser(function(user, done) { done(null, user); }); passport.deserializeUser(function(obj, done) { done(null, obj); }); passport.use(new LocalStrategy( function(username, password, done) { console.log("LocalStrategy working..."); return done(null, { id: 1, username: 'Joe', password: 'schmo'}); } )); var app = express(); app.configure(function(){ app.use(express.static(__dirname + '/app')); app.use(express.cookieParser('big secret')); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.cookieSession()); app.use(flash()); app.use(passport.initialize()); app.use(passport.session()); app.use(app.router); }); app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); app.post('/login', passport.authenticate('local'), function (req, res) { console.log("authenticated...."); res.end(); }); app.listen(port); 
+6
source share
4 answers

All users of the new express.js (4.x and higher) together with passport.js may encounter the "Lack of credentials" problem only because by default POST data is not processed. To fix this, install body-parser npm install body-parser and use in your code:

 var bodyParser = require( 'body-parser' ); app.use( bodyParser.urlencoded({ extended: true }) ); 

Good point from @ivarni: app.use( bodyParser.urlencoded({ extended: true }) ); should be placed prior to entering any passport middleware.

+23
source

What does your index.html or login page look like? At your post, you need to make sure that you send at least something in the body with the username and password fields. If you send a message without them, you will receive a Missing credentials error message. If you want to change them, you can change the settings as shown in this manual .

You can verify this yourself by adding a route to fix the login error, and specify this route when calling passport.authenticate .

 app.post('/login', passport.authenticate('local', { failureRedirect: '/loginerror', failureFlash: true }), function(req, res) { res.redirect('/'); }); app.get('/loginerror') function(req,res) { console.log(req.flash('error')); res.redirect('/login'); } 

I modified your example to add the necessary forms. In addition, if there is any error, it is displayed on the login page. For example, if you simply enter a username rather than a password, you will see the "Missing credentials" error message. Hope this helps!

 var http = require('http'), express = require('express'), passport = require('passport'), LocalStrategy = require('passport-local').Strategy, flash = require('connect-flash'); var port = process.env.PORT || 8080; passport.serializeUser(function(user, done) { done(null, user); }); passport.deserializeUser(function(obj, done) { done(null, obj); }); passport.use(new LocalStrategy( function(username, password, done) { console.log("LocalStrategy working..."); return done(null, { id: 1, username: 'Joe', password: 'schmo'}); } )); var app = express(); app.configure(function(){ app.use(express.static(__dirname + '/app')); app.use(express.cookieParser('big secret')); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.cookieSession()); app.use(flash()); app.use(passport.initialize()); app.use(passport.session()); app.use(app.router); }); app.get('/', function(req, res){ var username = "not logged in"; if (req.user) { username = req.user.username; } var body = '<html><body>'; body = body + '<p>' + username + '</p>'; body = body + '<a href="/login">login</a>' body = body + '</body></html>' res.send(body); }); app.get('/login', function(req, res){ var message = req.flash('error'); var body = '<div><p>' + message + '</p></div>'; body = body + '<form action="/login" method="post">'; body = body + '<div><label>Username:</label>'; body = body + '<input type="text" name="username"/><br/></div>'; body = body + '<div><label>Password:</label>'; body = body + '<input type="password" name="password"/></div>'; body = body + '<div><input type="submit" value="Submit"/></div></form>'; res.send(body); }); app.post('/login', passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }), function(req, res) { res.redirect('/'); }); app.listen(port); 
+16
source

For those who still get this error, double check the fields you submitted are really username and password . If not, you need to pass additional parameters as indicated in the documentation . for instance

 passport.use( new passportLocal({ usernameField: 'email', passwordField: 'passwd' }, func..)); 
+1
source

I do not think that the existing answers clearly explain the problem, which is as follows: “Passport local strategy” will complain about the lack of credentials if req.body.username and req.body.password .

Often the error is that the POST data has not been parsed, and this can be fixed using body-parser .

0
source

Source: https://habr.com/ru/post/947155/


All Articles