express无法验证

时间:2015-10-26 07:13:50

标签: node.js authentication

我一直关注微软视觉学院(mva)完成的平均堆栈视频。我对MVA模块3感到震惊。当我输入用户名和密码时,它总是重定向到失败路由。

passport.js:

passport.use('signup', new LocalStrategy({
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },
    function(req, username, password, done) {

        if (users[username]){
            console.log('User already exists with username: ' + username);
            return done(null, false);
        }

        //store user in memory 
        users[username] = {
            username: username,
            password: createHash(password)
        }

        console.log(users[username].username + ' Registration successful');
        return done(null, users[username]);
    })
);

authenticate.js:

 router.get('/success', function(req, res){
    res.send({state: 'success', user: req.user ? req.user : null});
});

//sends failure login state back to angular
router.get('/failure', function(req, res){
    res.send({state: 'failure', user: null, message: "Invalid username or password"});
});

//log in
router.post('/login', passport.authenticate('login', {
    successRedirect: '/auth/success',
    failureRedirect: '/auth/failure'
}));

enter image description here

我实际上遵循了https://github.com/hwz/chirp/tree/master/module-3

中给出的步骤

感谢任何帮助。在此先感谢:)

2 个答案:

答案 0 :(得分:0)

我认为你应该尝试将Content-Type更改为application/x-www-form-urlencoded

答案 1 :(得分:0)

好的,我已经为你找到了解决方案。不要通过标头但通过有效负载发送数据。是的,它很简单。

顺便提一下,我建议你签入passport.js登录方法。他们的Github存储库中有拼写错误。他们忘记了

中的!符号

if(users[username]){ console.log('User Not Found with username '+username); return done(null, false); }

所以它应该是:

if(!users[username]){ console.log('User Not Found with username '+username); return done(null, false); }