护照节点.js CORS 错误

Passport Node.js CORS error

本文关键字:CORS 错误 js 节点      更新时间:2023-09-26

我已经尝试了一百万种方法让护照与我的应用程序一起使用,但无济于事。每次尝试登录每个提供商(Facebook,Google,Twitter,Microsoft)都会导致如下错误:

XMLHttpRequest cannot load https://www.google.com/accounts/o8/ud?openid.mode=checkid_setup&openid.ns=h…2F%2Ftest.sl%2Fauth%2Fgoogle%2Freturn&openid.realm=http%3A%2F%2Ftest.sl%2F. 
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://test.sl' is therefore not allowed access.

我的应用程序并不复杂,这是我的服务器代码摘要。

var express = require('express');
var ppGoogle = require('passport-google-oauth').OAuth2Strategy;
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
//There's more config
app.listen(7230);
app.get('/auth/google', passport.authenticate('google'));
app.get('/auth/google/return', passport.authenticate('google', {
   successRedirect: '/main',
   failureRedirect: '/login'
}));
passport.use(new ppGoogle({
    clientID: '',
    clientSecret: '',
    callbackURL: 'http://test.sl/auth/google/return'
},
function (accessToken, refreshToken, profile, done)
{
    console.log('done');
}));

有人知道解决方案吗?这件事快把我逼疯了。

您正在尝试通过AJAX而不是页面导航加载Google的OAuth提示符。

您应该将 AJAX 请求替换为普通导航,