2017-02-16 75 views
0

我对我正在设计的简单应用程序的体系结构有怀疑。谷歌Oauth实施使用MEAN堆栈

我休息基于API服务器是在节点是在http://localhost:3000 我的客户是在http://localhost:4200

写在角2在开发我的休息节点API服务器我成功实施,我使用http://localhost:3000/auth/google测试护照谷歌认证和 我得到重定向到谷歌登录页面,然后进一步登录后,我重定向到我的/配置文件服务的我的其余节点api

现在我想做的一样,但起点是我的角度客户端调用节点api服务器调用google auth。所以我最初的请求从 http://localhost:4200,它做一个http.get到 http://localhost:3000/auth/google。希望谷歌的身份验证页面显示为我进行认证,但我得到下面的错误

XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_ur …=1000090953925-p7jof0qa284ihknb5sor3i4iatnqarvo.apps.googleusercontent.com. Redirect from ' https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_ur …=1000090953925-p7jof0qa284ihknb5sor3i4iatnqarvo.apps.googleusercontent.com' to ' https://accounts.google.com/ServiceLogin?passive=1209600&continue=https://a …sercontent.com%26from_login%3D1%26as%3D60339aeceb428c&oauth=1&sarp=1&scc=1' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

我已经提供的网址与无论是在谷歌身份验证页面的端口授权的JavaScript源 http://localhost:3000 http://localhost:4200 和下授权的重新导向的URI 我已经包括 http://localhost:3000/auth/google/callback

下面在节点API服务器代码

app.get('/ AUT h/google',passport.authenticate('google',{scope:['profile','email']})); app.get( '/ AUTH /谷歌/回调', passport.authenticate( '谷歌',{ successRedirect: '/简档', failureRedirect: '/用户' }), );

任何帮助表示赞赏

回答

0

两件事情:因为后端和前端是你需要让你的后端不同的端口包括你的反应的CORS header 'Access-Control-Allow-Origin'。像这样的东西,如果你正在使用快递:

res.header('Access-Control-Allow-Origin', '*'); 

而且,只要确保在浏览器的地址是不断HTTP://本地主机,而不是文件://本地主机

+0

嗨Roy,我不希望api服务器能够被任何其他应用程序调用,除了前端,所以我有下面的api服务器,只允许我的客户端应用程序运行在port4200上res.header('Access- Control-Allow-Origin','http:// localhost:4200'); ('Access-Control-Allow-Methods','GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers','Content-Type'); –

+0

嘿Vinay,如果你指定一个域名,你也应该添加协议。因此,在localhost:4200前添加http://。 –

+0

不工作,然后我添加了*但仍然与重定向问题XMLHttpRequest无法加载http:// localhost:3000/auth /谷歌。从'http:// localhost:3000/auth/google'重定向到'https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_ur...=1000090953925-p7jof0qa284ihknb5sor3i4iatnqarvo.apps.googleusercontent.com'已被CORS策略阻止:请求需要预检,这是不允许跟踪跨源重定向的。 –

0

所有你需要做的就是 发送$window.location.href="URL"

希望它可以帮助