No ‘Access-Control-Allow-Origin’ header is present on the requested resource, but the server registers a new user

  docker, laravel, next.js, nginx

I can’t figure out where the issue stems from.
I have a registration page for a new user at localhost:3000/register.
It calls the backend Laravel Sanctum API at localhost:8080/api/register.

Everything is set correctly in the Laravel configuration. The user even registers to the database, yet I still get this error:

Access to XMLHttpRequest at ‘http://localhost:8080/api/register’ from
origin ‘http://localhost:3000’ has been blocked by CORS policy: No
‘Access-Control-Allow-Origin’ header is present on the requested
resource

I saw many posts here but none of them has my issue because I seem to have everything configured properly.

So it might be due to the fact I am using Docker+Nginx as proxy.

This is my cors.php file:

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

'allowed_methods' => ['*'],

'allowed_origins' => ['http://localhost:3000'],

'allowed_origins_patterns' => ['*'],

'allowed_headers' => ['*'],

'exposed_headers' => [],

'max_age' => 0,

'supports_credentials' => true

This is my frontend register.jsx registration part:

axios.get('http://localhost:8080/sanctum/csrf-cookie',{withCredentials: true}).then(response => {        
    axios.post(
        'http://localhost:8080/api/register',
        {
            email: email,
            password: password
        },
        {
            withCredentials: true,
            headers: {
                'Content-Type': 'application/json',
                Accept: 'application/json',
            },
        }
    )
    .catch(function (error) {                         
          console.log(error);    
        })
    });

This is my nginx config file:

server {
    listen 8080;
    index index.php index.html;    
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ .php$ {        
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    
}

  server {    
    listen       80;         
    server_name mysite.test;
    location / {      
      proxy_pass      http://node:3000;
      
    }
  }

What could cause this issue? It’s weird because it shows this error yet the server registers the new user.
If any more information is needed let me know

Source: Docker Questions

LEAVE A COMMENT