AgonK
AgonK
FFilament
Created by AgonK on 9/15/2024 in #❓┊help
auth()->user cannot be read after login in middleware ?
@Dennis Koch
7 replies
FFilament
Created by AgonK on 9/15/2024 in #❓┊help
auth()->user cannot be read after login in middleware ?
@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!
7 replies
FFilament
Created by AgonK on 9/15/2024 in #❓┊help
auth()->user cannot be read after login in middleware ?
@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
7 replies