CORS Issue with React app and Laravel API

CORS Issue with React app and Laravel API

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving CORS Blocking in a Laravel API and React Frontend

You are encountering a classic cross-origin resource sharing (CORS) error because your browser is enforcing strict security policies between your React development server at http://localhost:3000 and your Laravel API endpoint. The specific error message indicates that the response from http://localhost/blog/public/api/auth/signin lacks the Access-Control-Allow-Origin header. Without this header, the browser blocks the response to prevent your frontend from accessing data originating from a different origin, even if both are running on localhost.

This is not a network connectivity issue; it is an HTTP header configuration problem. Your server responded with status 200 (presumably), but the headers included in that response did not explicitly grant permission for http://localhost:3000 to read them. The screenshot of your response headers confirms this absence.

Why htaccess Is Not Enough

You mentioned attempting to resolve this via .htaccess. While Apache's mod_headers can add custom headers, relying on it in a modern Laravel application is generally discouraged for two reasons: performance and maintainability. Adding headers directly in .htaccess bypasses Laravel's middleware stack, making it harder to manage dynamic CORS rules (e.g., allowing specific origins in development but only trusted domains in production). Furthermore, if you are using Nginx as a reverse proxy or running the API behind a load balancer, .htaccess directives simply won't be processed.

The industry-standard solution for Laravel is the barryvdh/laravel-cors package. This package provides middleware that sits within your application's request lifecycle, allowing you to define CORS rules programmatically in config/cors.php. This approach ensures consistency regardless of the underlying web server.

Implementing the Solution with Laravel CORS

The most robust way to fix this is to install the barryvdh/laravel-cors package. First, run the following command in your terminal:

composer require barryvdh/laravel-cors

Once installed, you need to publish the configuration file and update it with the correct origins. Run:

php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"

Open config/cors.php. You will see a paths array defining which routes the policy applies to. Since your API is mounted at /blog/public/api/, you must ensure the path matches exactly or use a wildcard if appropriate for your setup:

'paths' => ['api/*', 'sanctum/csrf-cookie'],

Crucially, look for the allowed_origins configuration. In a local development environment where both your React app and Laravel API are on localhost, you can allow all origins temporarily to get past the error:

'allowed_origins' => ['*'], // Use this only for local development

For production, replace ['*'] with your specific frontend domain, such as ['https://your-app.com']. Never use * in a production environment that accepts credentials (cookies), or you risk exposing sensitive data to any website.

Configuration Nuances and Trade-offs

There is an important distinction between allowing origins and handling credentials. If your API relies on session authentication (which Laravel often does by default) or sends cookies, you must set supports_credentials to true. However, if you enable credentials, you strictly cannot use ['*'] for allowed origins; the browser will reject the request immediately in that case.

Your specific setup involves mounting the API at a subdirectory path (/blog/public/api/). Ensure your React app is aware of this base URL. In your src/index.js, you might need to configure axios or fetch requests:

const API_URL = 'http://localhost/blog/public/api';
axios.defaults.baseURL = API_URL;

If you are mounting the Laravel API route group with a prefix, that prefix must align with what the frontend calls. Mismatched paths often lead to 404s or unexpected behavior even after fixing CORS headers.

Final Steps for Local Development

Since you are likely in a development environment given the localhost addresses, setting allowed_origins to ['*'] is the pragmatic choice. This tells the browser: "For now, trust any origin trying to talk to this server." Once you deploy the React app to its own domain and the Laravel API to production, you can tighten the security policy in config/cors.php to only allow the specific production domains.

Remember that after modifying configuration files in Laravel, you do not need to restart the server for these changes to take effect, as they are read dynamically unless you clear config cache. However, clearing the config cache is good practice:

php artisan config:clear

With the middleware correctly configured and the config/cors.php file updated with the appropriate origins and credentials settings, your React app should be able to successfully fetch data from your Laravel API without triggering the browser's security block.

Our network