do you have a SESSION_DOMAIN configured?
HTTP 419 form response w/SESSION_DRIVER=database
My login form was working fine when hosted on an EC2 using SESSION_DRIVER=file. I've deployed that same app to Laravel Vapor and made some necessary changes, including SESSION_DRIVER=database and now in my production environment when trying to log in w/a blade form I always get an HTTP 419. Could someone help me figure out the cause of this?
The login form does have the @csrf token included
<form method="POST" action="{{ route('admin-login') }}">
@csrf
<div class="form-group row">
....
One of the changes I made before this happened was I had to change my SESSION_DRIVER to database instead of file because Laravel Vapor runs on a Lambda and not a persisted disk
SESSION_DRIVER=database
Whenever I submit the login form I get this response
419 Page Expired
Any help is appreciated, thanks!
UPDATE 1
I've found that this is happening inside the VerifyCsrfToken class tokensMatch() method, the code in Laravel is here
-
$request->input("_token")is set correctly as the token in the HTML form (from@csrfin the blade template) -
$request->session()->token()is set to something else entirely, causingtokensMatch()to be false
Still not sure why request->session()->token() is set when I'm currently not logged in, and how it got set to a wrong token.
UPDATE 2
It seems SESSION_DRIVER=database is causing the issue. Every request gets a new session _token and all rows in the sessions table have user_id set to null. It seems sessions stored in the database aren't persisting for each user. Anyone have a clue why?
Solution
- in
session.phpI set "same_site" to "lax"
'same_site' => "lax",
- In Kernel.php the
StartSessionmiddleware was included in both$middlewareand$middlewareGroupsfor "web". This was causing it to runStartSessiontwice and overwrite the existing session with a new one for each request. Removing the duplicate in$middlewareGroups"web" fixed it.
protected $middleware = [
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Fruitcake\Cors\HandleCors::class,
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
------> these two below were the issue. removing them fixed it <------
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
],
];
Please or to participate in this conversation.