when you log in, the session is regenerated so any previous csrf token is no longer valid.
I would be very surprised if this worked in 5.5 either
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I created a form which is public. However, it requires the user to be logged in before it can be submitted.
Basically, I combined 2 forms on the same page: login form, and the main form.
I'm using AJAX to authenticate user, and sending the main form on success without refreshing the page.
$(function(){
$('#login-form').submit();
$('#login-form').on('submit', function(e){
e.preventDefault();
});
});
var loginForm = $("#login-form");
loginForm.submit(function(e){
e.preventDefault();
var loginData = loginForm.serialize();
$.ajax({
type: "POST",
url:'/login',
data : loginData,
success : function(data){
$("#main-form").submit();
}
});
});
It works fine with Laravel 5.5, but Laravel 5.6 gives me "The page has expired due to inactivity" error. Authentication works fine, and the user is logged in after refreshing the page. But the form cannot be submitted without refreshing the page.
Trying to submit the form using AJAX request after AJAX login console shows 419 error.
Of course, I'm using {{ csrf_field() }}. The form contains _token field.
Moreover, it works fine when I try to register a new user, and submit the form on success. Form submits successfully, and it belongs to a user.
It also works fine if I disable csrf protection for my main form route.
But how can I solve this problem with scrf? Maybe I have missed something?
Thank you, @Snapey , you are right.
Laravel has sendLoginResponse function which regenerates user's session.
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
You can override this function inside LoginController.php, and either delete this line: $request->session()->regenerate();, or return the new token:
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
if($request->ajax()){
// If request from AJAX
return [
'_token' => Session::token(),
];
} else {
// Normal POST do redirect
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
}
Please or to participate in this conversation.