admin管理员组文章数量:1026912
I'm using the Node.js Passport module to build an authentication process, and I'm unable to figure out why the verification always fails, even when I return success every time from the verification callback. To keep the example simple, I'm just using the passport-local strategy with no persistent storage:
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var express = require('express');
var server = express();
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (id, done) {
done(null, id);
});
passport.use(new LocalStrategy(
function (username, password, done) {
// Would perform lookup and verification here.
// Instead return a valid user object every time.
var user = { username: username };
return done(null, user);
}
));
server.post('/login', passport.authenticate('local', { failureRedirect: '/failure' }), function (req, res) {
res.send('access granted');
});
var port = process.env.PORT || 3000;
server.listen(port, function() {
console.log('Listening on port ' + port);
});
Similar questions have been solved by adding placeholder user serialization/deserialization methods, but I've got those in place.
Here's a CURL call to hit the above with a username and password:
curl -X "POST" "http://127.0.0.1:3000/login" \
--data-urlencode "username=alice" \
--data-urlencode "password=supersecret"
When I perform that POST, the response contains the HTTP 302 failure redirect to /failure
every time, even though I'm returning null
(no error), and a dummy user object in the LocalStrategy
callback. What am I overlooking?
I'm using the Node.js Passport module to build an authentication process, and I'm unable to figure out why the verification always fails, even when I return success every time from the verification callback. To keep the example simple, I'm just using the passport-local strategy with no persistent storage:
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var express = require('express');
var server = express();
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (id, done) {
done(null, id);
});
passport.use(new LocalStrategy(
function (username, password, done) {
// Would perform lookup and verification here.
// Instead return a valid user object every time.
var user = { username: username };
return done(null, user);
}
));
server.post('/login', passport.authenticate('local', { failureRedirect: '/failure' }), function (req, res) {
res.send('access granted');
});
var port = process.env.PORT || 3000;
server.listen(port, function() {
console.log('Listening on port ' + port);
});
Similar questions have been solved by adding placeholder user serialization/deserialization methods, but I've got those in place.
Here's a CURL call to hit the above with a username and password:
curl -X "POST" "http://127.0.0.1:3000/login" \
--data-urlencode "username=alice" \
--data-urlencode "password=supersecret"
When I perform that POST, the response contains the HTTP 302 failure redirect to /failure
every time, even though I'm returning null
(no error), and a dummy user object in the LocalStrategy
callback. What am I overlooking?
1 Answer
Reset to default 7I was overlooking two things:
- There was no call to the
passport.initialize()
middleware - I wasn't parsing request bodies because Express doesn't include that out of the box
Now my require block at the top includes both of those missing items, and it returns 200 OK when POSTing to /login
:
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var express = require('express');
var bodyParser = require('body-parser');
var server = express();
server.use(passport.initialize());
//server.use(passport.session()); -- For persistent login sessions
server.use(bodyParser.urlencoded({ extended: true }))
I'm using the Node.js Passport module to build an authentication process, and I'm unable to figure out why the verification always fails, even when I return success every time from the verification callback. To keep the example simple, I'm just using the passport-local strategy with no persistent storage:
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var express = require('express');
var server = express();
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (id, done) {
done(null, id);
});
passport.use(new LocalStrategy(
function (username, password, done) {
// Would perform lookup and verification here.
// Instead return a valid user object every time.
var user = { username: username };
return done(null, user);
}
));
server.post('/login', passport.authenticate('local', { failureRedirect: '/failure' }), function (req, res) {
res.send('access granted');
});
var port = process.env.PORT || 3000;
server.listen(port, function() {
console.log('Listening on port ' + port);
});
Similar questions have been solved by adding placeholder user serialization/deserialization methods, but I've got those in place.
Here's a CURL call to hit the above with a username and password:
curl -X "POST" "http://127.0.0.1:3000/login" \
--data-urlencode "username=alice" \
--data-urlencode "password=supersecret"
When I perform that POST, the response contains the HTTP 302 failure redirect to /failure
every time, even though I'm returning null
(no error), and a dummy user object in the LocalStrategy
callback. What am I overlooking?
I'm using the Node.js Passport module to build an authentication process, and I'm unable to figure out why the verification always fails, even when I return success every time from the verification callback. To keep the example simple, I'm just using the passport-local strategy with no persistent storage:
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var express = require('express');
var server = express();
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (id, done) {
done(null, id);
});
passport.use(new LocalStrategy(
function (username, password, done) {
// Would perform lookup and verification here.
// Instead return a valid user object every time.
var user = { username: username };
return done(null, user);
}
));
server.post('/login', passport.authenticate('local', { failureRedirect: '/failure' }), function (req, res) {
res.send('access granted');
});
var port = process.env.PORT || 3000;
server.listen(port, function() {
console.log('Listening on port ' + port);
});
Similar questions have been solved by adding placeholder user serialization/deserialization methods, but I've got those in place.
Here's a CURL call to hit the above with a username and password:
curl -X "POST" "http://127.0.0.1:3000/login" \
--data-urlencode "username=alice" \
--data-urlencode "password=supersecret"
When I perform that POST, the response contains the HTTP 302 failure redirect to /failure
every time, even though I'm returning null
(no error), and a dummy user object in the LocalStrategy
callback. What am I overlooking?
1 Answer
Reset to default 7I was overlooking two things:
- There was no call to the
passport.initialize()
middleware - I wasn't parsing request bodies because Express doesn't include that out of the box
Now my require block at the top includes both of those missing items, and it returns 200 OK when POSTing to /login
:
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var express = require('express');
var bodyParser = require('body-parser');
var server = express();
server.use(passport.initialize());
//server.use(passport.session()); -- For persistent login sessions
server.use(bodyParser.urlencoded({ extended: true }))
本文标签: javascriptNode passportlocal strategy always failsStack Overflow
版权声明:本文标题:javascript - Node passport-local strategy always fails - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745652273a2161388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论