最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passport Node.js CORS error - Stack Overflow

programmeradmin1浏览0评论

I've tried a million ways to get passport to work with my application to no avail. Every attempted login to every provider (Facebook, Google, Twitter, Microsoft) has resulted in an error like this one:

XMLHttpRequest cannot load .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 '' is therefore not allowed access.

My application isn't that plicated, here's a summary of my server code.

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: ''
},
function (accessToken, refreshToken, profile, done)
{
    console.log('done');
}));

Anyone know the solution? This thing is driving me crazy.

I've tried a million ways to get passport to work with my application to no avail. Every attempted login to every provider (Facebook, Google, Twitter, Microsoft) has resulted in an error like this one:

XMLHttpRequest cannot load https://www.google./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.

My application isn't that plicated, here's a summary of my server code.

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

Anyone know the solution? This thing is driving me crazy.

Share Improve this question edited Jan 12, 2015 at 0:23 SLaks 889k181 gold badges1.9k silver badges2k bronze badges asked Jan 11, 2015 at 23:43 Jack GuyJack Guy 8,5238 gold badges60 silver badges88 bronze badges 6
  • Where is that error ing from? It shouldn't be doing that in the first place. – SLaks Commented Jan 11, 2015 at 23:50
  • Sorry didn't specify, this is client-side with the browser that's trying to authenticate. – Jack Guy Commented Jan 11, 2015 at 23:51
  • What's the stack trace making that AJAX request? What client code is doing that? – SLaks Commented Jan 11, 2015 at 23:57
  • That's all automated by Passport. I don't know the details. The client is simply sending a GET request to /auth/google – Jack Guy Commented Jan 12, 2015 at 0:00
  • 1 That's wrong; you need to navigate to that URL, not AJAX-request it. – SLaks Commented Jan 12, 2015 at 0:01
 |  Show 1 more ment

1 Answer 1

Reset to default 9

You're trying to load Google's OAuth prompt over AJAX instead of a page navigation.

You should replace your AJAX request with a normal navigation,

发布评论

评论列表(0)

  1. 暂无评论