【发布时间】:2021-06-16 17:43:56
【问题描述】:
我在 laravel 中遇到 CSRF 令牌不匹配问题。我也检查了来自 stackflow 和其他站点的所有注释,但无法清除此问题。
https://mybestlife.mg-wellness.com/admin/login
用户:admin@gmail.com 通过:1234
您可以在控制台中检查错误。
阿贾克斯
$("#loginform").on('submit', function(e){
e.preventDefault();
$('button.submit-btn').prop('disabled', true);
$('.alert-info').show();
$('.alert-info p').html('Authenticating...');
$.ajax({
type:"POST",
url:$(this).prop('action'),
data:new FormData(this),
headers: headers,
dataType:'JSON',
contentType: false,
cache: false,
processData: false,
success: function(data) {
console.log(data);
if ((data.errors)) {
$('.alert-success').hide();
$('.alert-info').hide();
$('.alert-danger').show();
$('.alert-danger ul').html('');
for(var error in data.errors) {
$('.alert-danger p').html(data.errors[error]);
}
} else {
// console.log(data)
$('.alert-info').hide();
$('.alert-danger').hide();
$('.alert-success').show();
$('.alert-success p').html('Success !');
window.location.href = data;
}
$('button.submit-btn').prop('disabled',false);
}
});
});
登录功能
public function doLogin(Request $request)
{
// print_r($request->all());
$rules = [
'email' => 'required|email',
'password' => 'required'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(array('errors' => $validator->getMessageBag()->toArray()));
}
//--- Validation Section Ends
// Attempt to log the user in
if (Auth::guard('web')->attempt([
'email' => $request->email,
'password' => $request->password,
'status' => 1,
'role' => 1,
'level' => 0
], $request->remember)) {
// if successful, then redirect to their intended location
return response()->json(route('dashboard'));
}
// if unsuccessful, then redirect back to the login with the form data
return response()->json(array('errors' => [
0 => 'Credentials Doesn\'t Match !'
]));
}
代码在 localhost 和我的测试服务器上运行良好。但不是在服务器上,我在上面分享。 请帮我解决这个问题。
谢谢
【问题讨论】:
-
检查您的生产服务器时间。可能不同步
-
能发一下htaccess文件代码吗
-
我可以看到令牌正在生成每个请求,因此它会抛出错误。尝试重新启动服务器或检查 htaccess 文件或禁用 cookie 等。
-
服务器禁用 cookie 吗?