404 Not Found on sanctum/csrf-cookie path

Viewed 6017

So I've been building a projet based on laravel. I'm building on a SPA foundation with sanctum as my authorization package. It works perfectly. Then I deploy the project to the server and everytime I try to login there is a 404 error on /sanctum/csrf-cookie. How could this happen? Is it because the SanctumServiceProvider not working.

This is the error image

5 Answers

The problem is when you define sanctum prefix, the route become something else like this:

enter image description here

you can check your routes with : php artisan route:list

as you can see the /sanctum/ is removed and when you check the route /sanctum/csrf-cookie it will not be and throws 404 error. So you have two options:

  • add this prefix: 'prefix' => 'api/v1/sanctum'
  • or
  • change GET call to api/csrf-cookie

I solved this issue by adding:

AllowOverride All

to the apache directory config

add in last line inside config/sanctum.php

'routes' => false,

You need to check if you're setting correct axios defaults for your /sanctum/csrf-cookie api call.

You can set it as follows

    axios.defaults.withCredentials = true;
    axios.defaults.baseURL = "http://localhost"; //Before sending get request
    axios.get("/sanctum/csrf-cookie").then(async () => {
    //Logic to handle login
    });

If defaults are not set properly url becomes http::localhost::8080/sanctum/crf-cookie which is where frontend is serving but instead it has to be http::localhost/sanctum/csrf-cookie

Note: localhost example is just for explanation. For production server make sure your url is correct and api call is on backend.

Add in config/cors.php

'paths' => ['*']

Related