F
Filament2mo ago
AgonK

auth()->user cannot be read after login in middleware ?

I have created a middleware and placed in app.php file on laravel side. I try to read value in every request but the problem is that I can't read any property from auth() after login. Yes, on the first request the auth() does not exist, but after login it should be readable. Moreover, I have placed some data in the Session::put('some_data', $data), even these data do not exist(I checked with debug). Thank you anyway!
4 Replies
Dennis Koch
Dennis Koch2mo ago
Where did you use that middleware? It need to be after the Authenticate middleware.
AgonK
AgonK2mo ago
@Dennis Koch I use this middleware on app.php like this
->withMiddleware(function (Middleware $middleware) {
$middleware->append([AuthMiddleware::class]);
})
->withMiddleware(function (Middleware $middleware) {
$middleware->append([AuthMiddleware::class]);
})
I don't have any other middleware on the laravel side. I also put in the filament PanelProvider in middleware section but it has different behavior, it does not reach after login and because of that I put it on app.php and its reach on every request as I expected! Additional information: I customize the login in filament. I extend the \Filament\Pages\Auth\Login class and modify the authenticate method.
class Login extends \Filament\Pages\Auth\Login
{
public function authenticate(): ?\Filament\Http\Responses\Auth\Contracts\LoginResponse
{
// custom login logic
.....
.....
$response = parent::authenticate();
Session::put('some_data', $data);
return $response;

}
class Login extends \Filament\Pages\Auth\Login
{
public function authenticate(): ?\Filament\Http\Responses\Auth\Contracts\LoginResponse
{
// custom login logic
.....
.....
$response = parent::authenticate();
Session::put('some_data', $data);
return $response;

}
In this step (after successfully login) I changed the database connection and save it in session to handle on next request. But I cannot read any of these data in middleware like (auth()->user(), sessions) and so. I don't know why it's happened. Did I miss something here ? Thank you anyway Dennis
Dennis Koch
Dennis Koch2mo ago
Hm, the session probably gets regenerated, but that should happen in authenticate() I think. Do normal sessions work? It's directly after Login that it doesn't work?
AgonK
AgonK2mo ago
@Dennis Koch yes I tested in authenticate() after save sessions, then get it and everything works fine. I also tested with hardcoded values ​​as from "session" just to check if in the any next request I can get the sessions values ​​but unfortunately they cannot be read on middleware in laravel side(app.php). I don't know why! Then, I put this middleware on panel provider in authMiddleware and works better but crashed on somewhere in AuthenticateSession in handle method in this if statment
if (! $request->hasSession() || ! $request->user() || ! $request->user()->getAuthPassword()) {
return $next($request);
}
if (! $request->hasSession() || ! $request->user() || ! $request->user()->getAuthPassword()) {
return $next($request);
}
I got this error SQLSTATE[42S02]: Base table or view not found: 1146 Table 'default_db.users' doesn't exist This code try to access user model in default database that it configured in .env. But I change the default database connection on the fly on authenticate method. I cannot understand what is wrong here, while I use same structure of login (change db connection on the fly) in the same project but without filament installed only with API. Dennis is there any bug or Im wrong in somewhere 😅 . Thank you anyway! @Dennis Koch
Want results from more Discord servers?
Add your server