How to use Laravel Sanctum with two different frontend and backend domains?
Asked Answered
M

2

3

Frontend: localhost:3002

Backend: sanctum.local

How to config these two fields in the env file:

SANCTUM_STATEFUL_DOMAINS=
SESSION_DOMAIN=

when I try to login I get 419 unknown status.

Miracidium answered 15/10, 2020 at 17:8 Comment(0)
B
2

add:

SANCTUM_STATEFUL_DOMAINS="localhost,127.0.0.1"
SESSION_DOMAIN=".sanctum.local"
Bike answered 15/10, 2020 at 22:24 Comment(0)
D
2

I trust this would help someone in the future.

I had a VueJs app (using the Quasar framework) as the frontend with Laravel Framework 10.48.4 as the backend both hosted using different domains.

This is the configuration for my local environment (.env):

APP_URL=http://localhost:8000 
FRONTEND_URL=http://192.168.100.4:9000 
SESSION_DOMAIN=192.168.100.4 
SANCTUM_STATEFUL_DOMAINS=192.168.100.4

The explanation for the above is here: https://www.youtube.com/watch?v=2zKoS8GsKK8&ab_channel=cdruc

For my production, I had the backend on a different domain and the frontend on netlify. The .env config:

APP_URL=https://<the-domain>.com
FRONTEND_URL=https://<subdomain>.netlify.app
SESSION_DOMAIN=.netlify.app
SANCTUM_STATEFUL_DOMAINS=https://<subdomain>.netlify.app

The explanation for this is here: https://laracasts.com/discuss/channels/laravel/laravel-sanctum-419-csrf-mismatch-with-vue-spa

I spent a lot of time debugging this. My problem was not adding https:// for the SANCTUM_STATEFUL_DOMAINS value.

Downstairs answered 22/4 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.