将本地护照与 restify 集成时遇到问题 [英] Having issues with integrating passport-local with restify
问题描述
我的示例应用如下
var util = require("util")
restify = require("restify"),
q = require("q"),
_ = require("lodash");
//Create Server
var server = restify.createServer({
name: "TestAuth"
});
server.use(restify.queryParser());
server.use(restify.bodyParser());
//Initialize Passport
var passport = require("passport"),
LocalStrategy = require("passport-local").Strategy;
server.use(passport.initialize());
passport.use(new LocalStrategy(
function(username, password, done) {
return done(null, "Test")
}));
//Session setup
server.post("/login", function(req, res, next) {
passport.authenticate("local", function(err, user, info) {
console.log(util.format("%s is logged in!", user))
res.send(200);
return next();
})(req, res, next);
});
server.listen(8080);
当我发出请求/login?username=test&password=test 时,它会触发身份验证回调,但user"是假的.当我只使用
When I make the request /login?username=test&password=test it hits the authenticate callback but "user" is false. When I just use
server.post("/login", passport.authenticate("local");
我收到了来自 restify 的错误请求响应.
I get a Bad Request response from restify.
推荐答案
此护照文档页面显示,在底部的自定义回调"部分中,对于您在主代码段中使用通行证的方式,请求方法应该是get",而不是post".我从该页面复制代码片段:
This passport documentation page shows, in the "Custom Callback" section at the bottom, that, for the way you are using passport in your main snippet, the request method should be a 'get', not a 'post'. I copy here the code snippet from that page:
app.get('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/users/' + user.username);
});
})(req, res, next);
});
该页面还有其他关于如何使用护照的示例,包括您尝试的本地"变体.
That page has other examples of how passport can be used, including the "local" variant that you attempted.
这篇关于将本地护照与 restify 集成时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!