I’m trying to make a login page using nuxt.js as Front-end and Laravel for the API.
But after I take the csrf token from sanctum-cookie, i can’t use it because of "CSRF token mismatch." . And I don’t know what to do…
Laravel info:
APP_URL=http://localhost:8080
FRONTEND_URL=http://localhost:3000
SANCTUM_STATEFUL_DOMAINS=http://localhost:8000,localhost:3000,localhost:8000,http://localhost:3000
8080 is the port for LAravel and 3000 is nuxt.
Those are the calls to the api and the login is retarded and return an error about CSRF and I have no idea why.
// calls from nuxt
async function validate() {
await axios.get('http://localhost:8080/sanctum/csrf-cookie', {
withCredentials: true
});
const token = useCookie('XSRF-TOKEN');
await axios.post('http://localhost:8080/login', {
'email': '[email protected]',
'password': '1234567890'
}, {
headers: {
'X-XSRF-TOKEN': token.value as string
}
})
}
cors.php config
'paths' => ['api/*', 'sanctum/csrf-cookie'],
'allowed_methods' => ['*'],
'allowed_origins' => [env('FRONTEND_URL', 'http://localhost:3000')],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,
2
Answers
After 3 days ... all i need to do was to add
withCredentials: true
for the login axios...Awesome! <3
set sanctumn.php :
then set env variable in .env file as given below:
then run these commands in laravel project:
then in your nuxt login.vue: