【发布时间】:2014-02-08 09:46:19
【问题描述】:
我从下面的链接了解了在 passport.js 中使用 authenticate 方法 http://passportjs.org/guide/authenticate/
在我的项目中,我有以下代码:
app.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { console.log(err); return next(err) }
if (!user) {
return res.json(400, info);
}
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.json(200, {user_id: user._id, url:"/user/home"});
});
})(req, res, next);
});
我在另一个文件中调用了 /login url,如下所示
$http.post('/login', $scope.user).
success(function(data, status, headers, config) {
$window.location.href= data.url ? data.url : '/';
$scope.view.loading = false;
}).error(function(data, status, headers, config) {
console.log(data);
$scope.view.loading = false;
$scope.view.submitted = true;
$scope.view.serverError=data.message ? data.message : "Server Error!";
});
我的疑问是,passport.authenticate 是如何知道用户凭据的。 在调用 http.post 的同时,我们还发送了 $scope.user。但这只是“数据”。 passport.authenticate 怎么会知道对象“用户”?
【问题讨论】:
标签: angularjs angularjs-scope angularjs-routing