Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passport Node.js CORS error

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.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.

My application isn't that complicated, 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.

like image 886
Jack Guy Avatar asked Feb 22 '26 19:02

Jack Guy


1 Answers

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,

like image 98
SLaks Avatar answered Feb 25 '26 08:02

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!