6

I'm having an issue trying to combine a Vue + Axios app with an express backend with Passport using the Google Strategy OAuth2.

My express server is on localhost:5000 and my vue-cli webpack dev server is on localhost:3000. I am using the webpack proxyTable option to proxy requests from /api to localhost:5000/api. Supposedly it also deals with cors. If I use a normal hyperlink in my vue app like <a href="/api/auth/google">Auth with Google</a>, it works as expected, however if I try to use axios like:

async authGoogle() {
  const response = await axios.get('/api/auth/google')
  console.log(response)
}

I get this error from the google api callback:

Failed to load https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2Fapi%2Fauth%2Fgoogle%2Fcallback&scope=profile%20email&client_id={client_id}: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed access.

These are the api routes:

app.get(
  '/api/auth/google',
  passport.authenticate('google', { scope: ['profile', 'email'] })
)

app.get(
  '/api/auth/google/callback',
  passport.authenticate('google'),
  function(req, res) {
    res.json({ success: true })
  }
)

How can I make axios work correctly in this situation?

johnpyp
  • 867
  • 2
  • 7
  • 13
  • 1
    https://stackoverflow.com/questions/43276462/cors-issue-while-requesting-access-token-google-oauth-2 – sideshowbarker Jan 09 '18 at 23:16
  • Damn. Alright, I've setup dynamic redirects back to page instead. Thanks for info though. – johnpyp Jan 10 '18 at 13:57
  • Can u plz mention how u resolved the prblem. I ve ran into same issue. – npr Mar 17 '18 at 22:16
  • 1
    @npr sadly this is correct. Google oauth doesn't have any kind of http request support yet. I ended up creating a "routing" page to determine status of login and routing to specific pages depending. – johnpyp Mar 17 '18 at 22:46
  • @JohnPaulPenaloza I ran into the same problem but just that the error was a bit different. It gave some network error on axios request. I did not understand the solution. Can you please elaborate – Emma Dec 16 '19 at 19:28
  • [https://stackoverflow.com/questions/59361530/how-to-configure-passport-facebook-using-react-redux] You can view my error on this – Emma Dec 16 '19 at 19:29

0 Answers0