Passport Node.js CORS错误

时间:2015-01-11 23:43:45

标签: javascript express cors passport.js

我已经尝试过百万种方式让护照与我的申请一起工作无济于事。每次尝试登录每个提供商(Facebook,谷歌,Twitter,微软)都会导致像这样的错误:

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');
}));

有人知道解决方案吗?这件事让我发疯。

1 个答案:

答案 0 :(得分:6)

您尝试通过AJAX而非网页导航加载Google的OAuth提示。

您应该使用普通导航

替换您的AJAX请求