Javascript 使用angularjs、nodejs和passportjs进行身份验证
我不能解决这个问题,我在网上搜索了很多,但没有任何帮助。 我在localhost:63447上运行frontEndangular,在localhost:3000nodejs上运行后端。 我有一个问题,只有当它是一个新的用户和节点必须创建它之前发回的响应。用户正在创建,但我在angular上得到一个空响应。 我通过angular发送邮件:Javascript 使用angularjs、nodejs和passportjs进行身份验证,javascript,node.js,asynchronous,callback,passport.js,Javascript,Node.js,Asynchronous,Callback,Passport.js,我不能解决这个问题,我在网上搜索了很多,但没有任何帮助。 我在localhost:63447上运行frontEndangular,在localhost:3000nodejs上运行后端。 我有一个问题,只有当它是一个新的用户和节点必须创建它之前发回的响应。用户正在创建,但我在angular上得到一个空响应。 我通过angular发送邮件: $scope.signIn = function(userToSignIn){ console.log(userToSignIn);
$scope.signIn = function(userToSignIn){
console.log(userToSignIn);
$http.post('http://localhost:3000/authenticate/signup',
userToSignIn)
.success(function(data, status, headers, config){
$scope.test = data;;
})
.error(function(data, status, headers, config){
console.log(data);
});
要在nodejs中创建函数,请执行以下操作:
router.post('/signup',
function(req, res, next) {
console.log(req.body);
passport.authenticate('local-signup', function(err, user, info) {
if (err) { return next(err); }//This one is working
else if (!user) { return res.json({status: req.session.flash.signupMessage}); }//work
else if(user) {
return res.status(200).json(user._id);//this is the problem
}
})(req, res, next);}),
以下是本地注册功能:
passport.use('local-signup', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) {
// User.findOne wont fire unless data is sent back
process.nextTick(function() {
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' : email }, function(err, user) {
// if there are any errors, return the error
if (err)
return done(err);
// check to see if theres already a user with that email
if (user) {
return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
} else {
// if there is no user with that email
// create the user
var newUser = new User();
// set the user's local credentials
newUser.local.email = email;
newUser.local.password = newUser.generateHash(password);
// save the user
newUser.save(function(err) {
if (err) {
console.log('error'); throw err;
}else
console.log('saved');
return done(null, newUser, req.flash('You were successly logged in'));
});
}
});
问题是会话未设置为您未呼叫?如果不是,请描述问题。我认为这是一个跨域问题。只有当我从另一台主机调用时,服务器的响应才为空。使用邮递员它正在工作。现在不知道为什么,但有时nodejs会给我一个200,而大多数时候我什么都得不到:POST/authenticate/signup--ms--您是否尝试过将express设置为允许cors请求?是的,我已经设置了标题。而且,它不仅在必须创建新用户时才起作用