spencerlepine / woofer

Dating app for pets - a full stack MERN project. Complete with CI/CD pipeline w/ Jest, GitHub Actions, Docker Hub, and AWS EC2
https://youtu.be/aiJhCoZRc78
2 stars 1 forks source link

signup endpoint CORS bug #93

Closed spencerlepine closed 2 years ago

spencerlepine commented 2 years ago

Overview

The ENTIRE app is working in production, except this error when signing up:

- Access to XMLHttpRequest at 'http://localhost:5000/api/signup' from origin
-  'http://localhost:3000' has been blocked by CORS policy: Response to
-  preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'
- header is present on the requested resource.
Screen Shot 2022-05-05 at 6 01 18 PM

Fix

The express add was using cors with headers, but there were DUPLICATE headers being set. Instead, I commented out and used just this middleware:

app.all("*", function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*")
  res.header(
    "Access-Control-Allow-Headers",
    "Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild"
  )
  res.header("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")

  if (req.method == "OPTIONS") {
    res.send(200)
  } else {
    next()
  }
})

Ticket

Trello Ticket: #136