I've built a small web app locally using laravel 8 sanctum and vue.
Both locally and on the prod server I'm using docker so everything is the same. The code is running on a subdomain sub.my-domain.com
All works fine locally.
The issue I have is when running the code on live server.
I first send a request too /sanctum/csrf-cookie which returns a 204 response code but no cookies are set - this is where the issue is I believe as when running this locally the XSRF-TOKEN is set.
Login method within VueX:
login: function ({ commit }, data) {
axios.get('/sanctum/csrf-cookie', { withCredentials: true })
.then(response => {
axios.post('api/login', {
email: data.email,
password: data.password
})
.then(response => {
if (response.data.success) {
commit('setUser', response.data);
// router.go('/sales-dashboard')
} else {
console.log(response.data.message)
}
})
.catch(function (error) {
console.error(error);
});
})
},
.env
SESSION_DRIVER=cookie
SANCTUM_STATEFUL_DOMAINS=my-domain.com
SESSION_DOMAIN=.my-domain.com
SESSION_LIFETIME=120
SESSION_SECURE_COOKIE=false;
config/sanctum.php
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
'%s%s',
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1,.my-domain.com, sub.my-domain.com',
env('APP_URL') ? ','.parse_url(env('APP_URL'), PHP_URL_HOST) : ''
))),
I'm not sure where I'm going wrong as it all works fine locally so it must be something to do with the config.
To clarify, the actual login endpoint works fine. But when going to a route behind the sanctum middleware I get an unauthenticated message.
Please let me know where I'm going wrong
Thank you