2

I'm trying to add google authentication with passport in my application, that has been generated using yeoman fullstack-generator.

Inside my login controller i have this request:

$scope.googleAuth = function(){
  $http.get('/auth/google');
};

but when i call this function i have this error:

XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=h…d=<my-client-ID-here>. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

I tried to fix that problem adding a middleware layer in my express.js file:

app.use(function (req, res, next) {

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

  // 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();
});

but it still not working. Is this the right solution? and is it safe?

1 Answer 1

10

I fixed the problem using an hyperlink instead of a button as following

<a target="_self" href="/auth/google" class="btn btn-danger">Google+</a>

I used target="_self"otherwise angular redirects to home page because, I think, it considers /auth/google as a route of my app.

Sign up to request clarification or add additional context in comments.

2 Comments

How are you passing the result of the auth (i.e. the user object) back to your controller?
+1 Life Saver..Spend more than 3hrs on this one. :) Thanks a ton.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.