我正在尝试在我的 Nodejs/Angular/Express/Passport 应用程序中通过 Facebook 实现 OAUTH 登录,但我对此很挣扎。
我仍然收到 CORS 错误:
XMLHttpRequest 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。起源 'https://www.xxxxxx.net https://www.xxxxxx.net' 因此不允许访问。
虽然我已经添加到我的 EXPRESS ROUTER:
router.all('/*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
if ('OPTIONS' === req.method) {
res.send(200);
}
else {
next();
}
});
在开发者控制台中,我可以看到“oauth/facebook”GET 调用的标头添加了“Access-Control-Allow-Origin”等。
在回调中没有“Access-Control-Allow-Origin”等 - 这是正确的吗?
router.get('/oauth/facebook/',passport.authenticate('facebook',{
failureRedirect: '/info',
scope:['email']
}));
router.get('/oauth/facebook/callback/', passport.authenticate('facebook',{
failureRedirect: '/info',
successRedirect: '/',
scope:['email']
}),
function(req,res){
if(req.user){
return res.json({token: req.user.generateJWT()});
} else {
return res.status(400).json({message:"Not found"});
}
});