我正在使用PassportJS运行MEAN堆栈进行身份验证,而我的注册模块与我的Angular控制器交互时遇到问题。基本上,从不调用errorCallback,我不确定如何正确使用Passport done()实现。
我有一个基本的注册表单,在提交后,调用此请求:
$http.post('/api/signup', {
name: $scope.user.name,
email: $scope.user.email,
password: $scope.user.password,
userSince: new Date().now
}).then(
function successCallback(res) {
$rootScope.message = 'Account Created';
console.log('Success'+res);
console.dir(res,{depth:5});
$location.url('/signupConf');
}, function errorCallback(res) {
$rootScope.message = 'Failure, see console';
console.log('Error: '+res);
console.dir(res,{depth:5});
$location.url('/');
});
快递路线:
app.post('/api/signup', passport.authenticate('local-signup'),function(req, res) {
console.log('User: ' + req.user.email);
});
最后,护照(改编自Scotch.io tut)模块略微删减:
passport.use('local-signup', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done) {
console.log("Signup Request: "+email);
process.nextTick(function() {
User.findOne({ 'email' : email }, function(err, user) {
if (err) { return done(err); }
// check to see if theres already a user with that email
if (user) {
console.log("User not created, already exsists: "+user);
return done(err, false, {message: 'Username already exsists.'});
} else {
// if there is no user with that email
// create the user
var newUser = new User();
//a bunch of data creation here
newUser.save(function(err) {
if (err) {throw err;}
console.log("Sucessfully created: "+newUser);
return done(null, newUser);
});
}
});
});
}));
一切运行正常,用户被创建纠正,如果存在给定电子邮件的用户,则不会在其上写入新的用户。但是,无论如何,都会调用successCallback。当用户名已存在时,我可以在浏览器控制台中看到401错误。当它的错误请求(即并非所有字段都填满)时,出现400错误。
所有服务器端console.logs工作正常,导致我认为我的角度前端有问题,或者后端如何响应请求。
(Scotch.io教程学分:https://scotch.io/tutorials/easy-node-authentication-setup-and-local)
答案 0 :(得分:0)
这个问题有点盯着我,这是在我的路线处理中。
app.post('/api/signup', function(req, res, next) {
passport.authenticate('local-signup', function(err,user,response) {
//handle responses based on state of user and err
})
(req, res, next);
});