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

 14835688_d4705c_859 发布于 2023-01-07 13:29

我正在尝试在我的应用程序中添加带有护照的google身份验证,这是使用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=. 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();
});

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

1 个回答
  • 我使用超链接而不是按钮修复了问题,如下所示

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

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

    2023-01-07 13:32 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有