Here's an older S.O. https://stackoverflow.com/questions/18166890/changing-cookie-expiration-with-laravel-4
Adapt to latest laravel code.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have some routes in Laravel for which I have middleware setup for simple cookie authentication. I basically ask for a password and if they enter the correct password, I set a cookie which allows them to view the page.
Unauthed flow
User goes to URL -> password page -> user lands at desired page
Authed flow
User goes to URL -> user lands at desired page
Controller
public function gate_check(Request $request)
{
$stored_password = Page::where('url', session('page'))->first()->password;
$password = $request->password;
if($password === $stored_password){
Cookie::queue(cookie::make(session('page'), 'true', 20));
return redirect()->route('upload', session('page'));
} else {
return back();
}
}
Middleware
public function handle($request, Closure $next)
{
if(Cookie::get($request->route('url'))){
return $next($request);
}
session(['page' => $request->route('url')]);
return redirect()->route('gate');
}
You may notice in the controller method that I set the cookie expiry to 20 minutes on this line
Cookie::queue(cookie::make(session('page'), 'true', 20));
The pages the users go to are single page vue applications that perform file uploads via ajax.
The problem
If a user spends longer than 20mins on the page, the cookie expires and any upload requests fail due to a CSRF mismatch (a page refresh is needed in order to trigger the auth flow to get a fresh cookie)
But doing a page refresh could potentially waste a lot of time for the user as it will void anything they've done in the page prematurely.
The question
How can I, with JS, extend/prolong/refresh the cookie from Laravel? Essentially I need a popup asking them to re-enter the password again... (which avoids a page refresh) which I know how to do except for the actual refreshing/setting of the cookie part.
Please or to participate in this conversation.