L5-Swagger icon indicating copy to clipboard operation
L5-Swagger copied to clipboard

CSRF token mismatch error

Open guigralho opened this issue 2 years ago • 6 comments

  • L5-Swagger Version: 8.5.0
  • PHP Version (php -v): 8.2.6
  • OS: ubuntu 22

When I make requests via the swagger documentation, the X-CSRF-TOKEN on header is null all the times and I get the SRF token mismatch error

Captura de tela de 2023-05-26 13-43-26

guigralho avatar May 26 '23 16:05 guigralho

Looks like documentation routes need middleware "web" php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider" and in config/l5-swagger.php: defaults-> middleware[ 'api' => ['web'], 'asset' => [], 'docs' => [], 'oauth2_callback' => [], ]

lpovkhan avatar Jun 05 '23 08:06 lpovkhan

in kernel file section api commit this middleare \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,

    'api' => [
     // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
        \Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

OsamaAlmamri avatar Sep 30 '23 00:09 OsamaAlmamri

Fixed this issue by updating file app/Http/Middleware/VerifyCsrfToken.php

Add /api/* item to $except array:

protected $except = [
  "/api/*"
];

quanghuybest2k2 avatar Oct 27 '23 07:10 quanghuybest2k2

@quanghuybest2k2 You're disabling the CRSF checks for all API routes. That is NOT recommended. CSRF tokens are important if API calls are made from browser/web.

vibonacci avatar Jan 01 '24 16:01 vibonacci

  • L5-Swagger Version: 8.5.0 -PHP Version (php -v): 8.2.12 -OS: windows -laravel/framework: 9.52.16

I have same errors. I try to fix that error according to your tips but I can't fix that error and I have same so.

Screenshot 2024-01-14 153950

JacksonPorter7478 avatar Jan 14 '24 23:01 JacksonPorter7478

Looks like documentation routes need middleware "web" php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider" and in config/l5-swagger.php: defaults-> middleware[ 'api' => ['web'], 'asset' => [], 'docs' => [], 'oauth2_callback' => [], ]

This works like charm. Looks the right and safe way. That's right?

marcelodmdo avatar Mar 17 '24 14:03 marcelodmdo