I was able to resolve this by adding \Illuminate\Session\Middleware\StartSession::class to the api middleware group in Kernel.php
Sanctum throws "Session store not set on request"
I've been developing a SPA with Sanctum locally, which works. Registration, verification and login. Now I moved to production, and registration and verification still work, but login throws:
Session store not set on request.
"/home/***/webapps/bwf-app/vendor/laravel/framework/src/Illuminate/Http/Request.php"
And that's the bit that confuses me. The get('sanctum/csrf-cookie') sets the cookie and apparently sets it correctly for the post to /api/v1/register to work. But then if I login, I get the error 500. What am I missing?
- Cors is set to 'api/*'
- Session driver is set to cookie
- Session domain is set to '.domain'
- Sanctum domain is set to a comma-delimited list
I revisited this recently and tested both stateless and stateful using Insomnia (Postman). So I added EnsureFrontendRequestsAreStateful without change, set the sanctum config file and cors config file as per documentation.
Next I created a single api.php route (/user > return auth()->user();) with auth:sanctum middleware applied.
Then in Insomnia I:
Stateless
- Request the /user route with a Bearer token in the request.
Stateful
- Request /sanctum/csrf-cookie and store the cookie in my Insomnia environment (cookie jar)
- Request /login with x-xsrf-token (cookie from step 1) and Referer: http://localhost:8000 in the Request header and user credentials in the body (form-data). I use Laravel Fortify as auth controller.
- Request the /user route with the x-xsrf-token and Referer in the Request header
In both cases I get the user associated with the token.
That to me solves my original challenge. Whether code has changed, or my understanding of stateless vs stateful has improved, you should now be able to get it working.
Please or to participate in this conversation.