我正在使用node.js restify和password模块,restify-ensure-login模块来执行用户登录身份验证功能。

https://www.npmjs.com/package/restify-ensure-login

这里是相关代码;

var passport = require('passport');
var Strategy = require('passport-local').Strategy;

passport.use(new Strategy(
    function(username, password, cb) {
        db.users.findByUsername(username, function(err, user) {
            if (err) { return cb(err); }
            if (!user) { return cb(null, false); }
            if (user.password != password) { return cb(null, false); }
            return cb(null, user);
        });
    }));

passport.serializeUser(function(user, cb) {
    cb(null, user.id);
});

passport.deserializeUser(function(id, cb) {
    db.users.findById(id, function (err, user) {
        if (err) { return cb(err); }
        cb(null, user);
    });
});

var server = restify.createServer({
    name: 'myapp',
    version: '1.0.0'
});

var api_post_login = function (app) {
    function respond(req, res, next) {
        res.redirect('/');
        return next();
    }; //function respond(req, res, next) {

    // Routes
    app.post('/login', passport.authenticate('local', { successReturnToOrRedirect: '/', failureRedirect: '/login' }) );
} //var api_get_login = function (app) {

api_post_login(server);


问题出在api_post_login()上。当执行HTTP发布以验证用户名和密码时,在登录页面上调用此功能时,将显示以下错误;

{
     code: "InternalError",
     message: "res.redirect() requires a next param (function) is required"
}

最佳答案

您需要使用passport-restify而不是passport。所以npm install passport-restify,然后将require更改为var passport = require('passport-restify');

关于javascript - Post API中的重定向错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34393300/

10-12 13:40