我一直关注微软视觉学院(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'
}));
我实际上遵循了https://github.com/hwz/chirp/tree/master/module-3
中给出的步骤感谢任何帮助。在此先感谢:)
答案 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);
}