我正在尝试进行跨域登录请求,并且除了登录之外的所有内容都在工作。因此,如果我去 api.example.com 并使用 laravel 应用程序登录,然后去 www.example.com 我可以通过调用 /api/user 端点(设置为气锁的一部分)来获取用户。我没有做的是使用用户名/密码通过 AJAX 登录。这是使用的JS:
首先我打电话setCSRFCookie()
function setCSRFCookie() {
let xhr = new XMLHttpRequest();
xhr.open('GET', domain+'/airlock/csrf-cookie');
xhr.withCredentials = true;
xhr.send(null);
}
然后我打电话loginUser()
function loginUser() {
let xhr = new XMLHttpRequest();
let params = 'username=m@example.com&password=password';
xhr.open('POST', domain + '/login', true);
xhr.withCredentials = true;
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
}
}
xhr.send(params);
}
我得到一个419 (unknown status)似乎是 Laravel 对无效 CSRF 令牌的响应。我查看了实际请求和两者APP_NAME_session,并且XSRF-TOKEN作为请求的一部分被传递。我唯一的想法/login是没有为此设置默认的身份验证路由,我需要在下面做一个,/api/login所以它由气锁中间件提供服务。感谢您提供的任何帮助。
Laravel 6.12.0 气闸 0.1.0