DayDev's avatar

Need help setting up Sanctum

I have a laravel api that I built and I am trying to create an SPA for it now. This is the first time I have done this so I am now learning about Sanctum.

I have read through the docs carefully and followed along this tutorial: https://laracasts.com/series/whats-new-in-laravel-7/episodes/6

But my API is still not authenticating me.

The Steps I Have Taken So Far

  • installed and published sanctum
  • added the right middleware to my Http/Kernam.php
  • updated config/cors.php so that supports_credentials=true and added the paths as specified in the tutorial
  • added the correct domain in my config/session.php file
  • updated my global axios: axios.defaults.withCredentials = true;
  • added axios.get('/sanctum/csrf-cookie') to my login form and confirmed that the proper cookies were set in my browser. This is in app.blade.php in the source code below.
  • confirmed that additional axios requests included these cookies in the headers
  • added a new route to api.php for testing: Route::middleware('auth:sanctum')->get('/user', function (Request $request) { return $request->user(); });

Now when I load my home page, I included an automatic call to this new route (in Home.vue in the code source listed below):

axios.get('/api/user').then(response => console.log(response))

Which returns a 401. I feel like I did everything in the tutorial up to this point, but I am still unauthenticated and I have no idea why.

My Code

You can see exactly what I did here in this commit. This includes everything I described above to set up sanctum.

Thank you for anyone who can help me.

0 likes
1 reply
DayDev's avatar
DayDev
OP
Best Answer
Level 8

Figured it out. Went back through the tutorial a second time and double checked each step. Somehow I didnt notice that you had to set SANCTUM_STATEFUL_DOMAINS in your env/config

Please or to participate in this conversation.