Skip to content

🐞 Bug: Registration Failing Due to CORS Policy Error #1

Description

@arnab9957

🐞 Bug: Registration Failing Due to CORS Policy Error

📌 Overview

User registration is currently failing because the backend API is
blocking requests from the frontend domain due to a CORS (Cross-Origin
Resource Sharing) misconfiguration.

When a user attempts to sign up, the browser blocks the request during
the preflight check.


🌐 Affected Endpoint

POST https://pharmacy-74zf.onrender.com/user/register

Frontend Origin: https://pharmacy-mu-lovat.vercel.app


🚨 Console Error

Access to fetch at 'https://pharmacy-74zf.onrender.com/user/register'
from origin 'https://pharmacy-mu-lovat.vercel.app' 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.

POST https://pharmacy-74zf.onrender.com/user/register net::ERR_FAILED
TypeError: Failed to fetch


❌ Current Behavior

  • Registration request fails.
  • Browser blocks the API call.
  • User sees: "An error occurred. Please try again."
  • Console shows CORS error and net::ERR_FAILED.

✅ Expected Behavior

  • Backend should allow requests from the deployed frontend.
  • Preflight OPTIONS request should return status 200.
  • Proper CORS headers should be included in API responses.
  • Registration should complete successfully.

🧠 Root Cause

The backend is not sending required CORS headers:

  • Access-Control-Allow-Origin
  • Access-Control-Allow-Methods
  • Access-Control-Allow-Headers

Because of this, the browser blocks cross-origin requests during the
preflight phase.


💡 Suggested Fix (Backend - Express Example)

1️⃣ Install CORS middleware

npm install cors

2️⃣ Configure CORS properly

const cors = require("cors");

app.use(cors({
  origin: "https://pharmacy-mu-lovat.vercel.app",
  methods: ["GET", "POST", "PUT", "DELETE"],
  credentials: true
}));

app.options("*", cors());

⚠️ For development only:

app.use(cors());

🖥️ Impacted Areas

  • User Registration (/user/register)
  • Login endpoint (potentially)
  • Any API route accessed from frontend

🧪 Acceptance Criteria

  • Registration works without console errors
  • No CORS error appears in browser
  • Preflight request returns 200
  • Access-Control-Allow-Origin header is present
  • Works in production (Render + Vercel deployment)
Image

🏷️ Labels

bug, backend, cors, api, high priority

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions