angularjs - 使用 javascript fullstack 进行 Passport google 身份验证

标签 angularjs express mean-stack passport.js

我正在尝试在我的应用程序中添加带有 Passport 的谷歌身份验证,该身份验证是使用 yeoman fullstack-generator 生成的。

在我的登录 Controller 中我有这个请求:

$scope.googleAuth = function(){
  $http.get('/auth/google');
};

但是当我调用这个函数时,我遇到了这个错误:

XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=h…d=<my-client-ID-here>. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

我尝试在我的express.js 文件中添加中间件层来解决该问题:

app.use(function (req, res, next) {

  // Website you wish to allow to connect
  res.setHeader('Access-Control-Allow-Origin', 'http://localhost:9000');

  // Request methods you wish to allow
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

  // Request headers you wish to allow
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

  // Set to true if you need the website to include cookies in the requests sent
  // to the API (e.g. in case you use sessions)
  res.setHeader('Access-Control-Allow-Credentials', true);

  // Pass to next layer of middleware
  next();
});

但它仍然不起作用。 这是正确的解决方案吗?安全吗?

最佳答案

我使用超链接而不是按钮解决了问题,如下所示

<a target="_self" href="/auth/google" class="btn btn-danger">Google+</a>

我使用了 target="_self"否则 Angular 重定向到主页,因为我认为它会将 /auth/google 视为我的应用程序的路径。

关于angularjs - 使用 javascript fullstack 进行 Passport google 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24448862/

相关文章:

javascript - Angular 2 中的 URL 路由

angularjs - 单元测试时如何在 Angular 1.5 中向 $componentController 添加依赖项

json - 如何从 casperjs 将数据发送回 Node ?

postgresql - 作为 JSON 对象与多个查询一起返回不同的查询

javascript - 使用外部 ng-repeat 的对象属性过滤嵌套的 ng-repeat

javascript - Kue:队列为空时通知的事件

javascript - typescript 错误 : Property 'append' does not exist on type 'HTMLElement'

angularjs - 使用 ui-router 在 AngularJS 中禁用模板缓存

node.js - 在 Azure 上部署 Nodejs 但收到 404

angular - 为什么在 Angular 中使用 Gulp