【问题标题】:Passport google authentication with javascript fullstack使用 javascript fullstack 进行护照谷歌身份验证
【发布时间】:2014-08-18 09:20:04
【问题描述】:

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

在我的登录控制器中,我有这个请求:

$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();
});

但它仍然无法正常工作。 这是正确的解决方案吗?安全吗?

【问题讨论】:

    标签: angularjs express mean-stack passport.js


    【解决方案1】:

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

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

    我使用 target="_self"otherwise 角度重定向到主页,因为我认为它认为 /auth/google 是我的应用程序的路由。

    【讨论】:

    • 您如何将身份验证的结果(即用户对象)传递回您的控制器?
    • +1 Life Saver..在这个上花费超过 3 小时。 :) 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 2012-09-20
    • 2018-02-13
    相关资源
    最近更新 更多