0

I am trying to login using google authentication using node.js, but it showing error like below.

XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_ur…d=410427634474-u3tpasmj4r80s6v20o54s85fikhotl79.apps.googleusercontent.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Someone please help me to solve this, because i am trying this from past two days, still didn't fix.

Kevin
  • 653
  • 2
  • 13
  • 34
  • Possible duplicate of ["No 'Access-Control-Allow-Origin' header is present on the requested resource"](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource) – Ben Fortune Jul 21 '16 at 09:52

2 Answers2

1

Try adding the following middleware to your NodeJS/Express app (I have added some comments for your convenience):

// Add headers
app.use(function (req, res, next) {

// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');

// 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();
});
Jaldhi Oza
  • 92
  • 5
  • After put this i am getting same error. Could you please see this link [this is what I actually doing](http://stackoverflow.com/questions/38472981/login-with-google-to-my-application-using-node-js-and-passport-authentication) – Kevin Jul 21 '16 at 10:12
  • @Kevin: can you please try as below: router.get('/auth/google/callback', function(req, res, next) { res.set('Access-Control-Allow-Origin', '*'); passport.authenticate('google', function(err, user, info) { if (user === false) { . . . . } }); }); – Jaldhi Oza Jul 21 '16 at 10:23
0

Write this in app.get function.

res.set('Access-Control-Allow-Origin', '*');

You can use node function as

app.get("/png",function(req,res){
     res.set('Access-Control-Allow-Origin', '*');
    var sr = { data: "", message: "", error: "" };
     console.log(222);
connection.query('SELECT * FROM png ', function(err, rows, fields) {
connection.end();
  if (!err)
  {
      var userr = rows;
      sr.data = userr;
      res.json(sr);

  } else{
    console.log('Error while performing Query.');
  }
  });
});
  • could you please see this link and tell me what's wrong with that.[That also I posted regarding this](http://stackoverflow.com/questions/38472981/login-with-google-to-my-application-using-node-js-and-passport-authentication) – Kevin Jul 21 '16 at 10:04