I get a "419 (unknown status)" error with the message "CSRF token mismatch."
POST http://127.0.0.1:8000/login 419 (unknown status)
Laravel server : http://127.0.0.1:8000
Vue server : http://localhost:8080
app/Http/Kernel.php
'api' => [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
app/Models/User.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
//...
}
config/cors.php
<?php
return [
'paths' => [
'api/*',
'sanctum/csrf-cookie',
'register',
'login',
],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,
];
.env
SESSION_DRIVER=cookie
SESSION_DOMAIN=localhost
SANCTUM_STATEFUL_DOMAINS=localhost:8080
src/main.js
axios.interceptors.request.use((config) => {
config.baseURL = 'http://127.0.0.1:8000'
config.withCredentials = true
return config
})
src/views/auth/Login.vue
import axios from 'axios'
import { reactive } from '@vue/reactivity';
export default {
setup() {
const credential = reactive({
email: '',
password: '',
})
const login = async () => {
axios.get('/sanctum/csrf-cookie').then( async () => {
let response = await axios.post('/login', credential)
console.log(response);
});
}
return { login, credential }
}
};
您已将SANCTUM_STATEFUL_DOMAINS
设置为localhost:8080
,但其余代码显示您在端口 8000 而不是 8080 上运行。如果您将其更改为 8000,您应该很高兴。
I had a hard time dealing with this same issue. After several searches, I landed on this page. Having seen possible suggested solutions, I changed
SANCTUM_STATEFUL_DOMAINS=localhost:8080
to
SANCTUM_STATEFUL_DOMAINS=http://localhost:8080
And that was it! It worked fine!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.