node.js - 配置 Passport 接受没有正文的请求?

标签 node.js passport.js

我正在尝试设置一个 LocalStrategy,可以在没有请求正文中给出的凭据的情况下登录

app.post('/signin', passport.authenticate('local-signin', {failureFlash : true}), function (req, res) {
    res.send(req.user);
});

策略如下:

passport
    .use('local-signin', new LocalStrategy({
        usernameField : 'email',
        passwordField : 'password',
        passReqToCallback : true
    }, 
    function (req, email, password, done) {
        if (email.length > 0 && password.legnth > 0) {
             /** Do some stuff with credentials **/
        }
        else {
             console.log('method2');
             /** Other method to log in with request headers...**/
        }
    });

不幸的是,当我向 /signin 发布没有正文的请求时,出现状态 400 错误,并且未加载策略(在示例中我没有 method2 在控制台中编写)。

有没有办法将 Passport 配置为接受没有正文的请求?

作为信息,我使用 AngularJS 前端,通过 $http.post('/signin'); 发出请求

Passport 版本:0.2.1

Passport 本地版本:1.0.0

最佳答案

passport.authenticate 中间件包装在另一个类似的东西中

app.post('/signin', myPass, function(req,res){res.send(req.user)});

function myPass(req, res, next){
    // do your thing
    passport.authenticate('local-signin', {failureFlash : true})(req, res, next);
    // ^ this returns a middleware that you gotta call here now ^
}

关于node.js - 配置 Passport 接受没有正文的请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27464091/

相关文章:

Android socket.io 应用程序无法连接到 node.js 服务器

node.js - 在express中禁用pug(jade)模板的 View 缓存

node.js - NodeJS Passport session 序列化 - 将用户反序列化到 session 中?

node.js - 注销没有 req 对象的用户 Passport js

javascript - Passport.js 登录错误

Node.js + Express : What do I have to serve the specific folder's path to express. 静态?

node.js - Node passport-jwt 从 token 中提取错误的用户

google-chrome-extension - 将 google 身份从 chrome 扩展程序传递到我的 node.js 应用程序

javascript - 此代码是否有可能丢失一些匹配项?

来自多个身份提供者的 Node.js Passport SAML