I have a laravel backend and a website on react/redux. I'm trying to pass my JWT token on a specific url from my frontend using superagent-bluebird-promise but I keep getting
No 'Access-Control-Allow-Origin' header is present on the requested resource
error. I have looked at every single article but couldn't figure it out. Here is the code on my frontend:
import request from 'superagent-bluebird-promise'
......
requestPromise= request.get(API_URL + action.url);
if(action.token)
requestPromise.set("Authorization","Bearer "+ action.token).withCredentials();
requestPromise.query({ // add query (if get)
...action.data,
});
On my laravel I have this .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
and I also have a CORS middleware to allow this header which it never gets to here for this code to run:
public function handle($request, Closure $next)
{
$origin = 'https://mywebsite.com';
return $next($request)
->header('Access-Control-Allow-Origin', $origin)
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
->header('Access-Control-Allow-Headers','Authorization')
->header('Access-Control-Allow-Credentials','true');
;
}
I'm also running my laravel in a docker/nginx setting if helps
I fixed this issue by adding my cors middleware to global middleware in kernel.php
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\Cors::class,
];