【发布时间】:2016-02-18 06:55:19
【问题描述】:
我在获取带有身份验证令牌的 cookie 作为请求的一部分传递时遇到问题。当我在完成身份验证请求后查看 Chrome 时,响应 cookie 已正确设置,但它从未传递到其他请求中。
生成 cookie 的 /login 的快速路由:
router.get('/login',
passport.authenticate('local', {
session: false
}),
function(req, res) {
console.log('/login authentication successful');
token.generate(req.users, function(result) {
res.cookie('access_token', result, {
//secure : true,
//maxAge : 1000*60*60, // 1 hour
path: '/',
httpOnly: true
});
res.json(Response.success(req.user));
});
}
);
我还有以下设置标题:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Credentials", true);
next();
});
现在在 Angular 中,我使用 withCredentials: true 将配置对象传递给我的 $http 请求,但到目前为止,没有任何效果。
$http({
method: 'GET',
url: 'http://localhost:3000/api/v1/devices',
withCredentials: true
}).then(function successCallback(response) {
// do stuff
}, function errorCallback(response) {
// oh noes!
});
关于我的设置的说明:我有一个节点服务器正在运行以在端口 3000 上提供后端,而另一台节点服务器使用 browserSync 在端口 8080 上显示客户端。我看到了一个建议,使用 http://localhost:8080 而不是IP 可能会导致 cookie 无法通过,但这似乎没有什么不同。
希望你们中的一个好人或女孩能对我的问题有所了解。谢谢!
【问题讨论】:
标签: javascript angularjs node.js cookies