Render Hook in middleware problem

Hello, I'm trying to show a custom message before login form with middleware if user is not active. But no success... I create a custom Middleware
CheckIsActive
CheckIsActive
to replace Authenticate in authMiddleware array of my
```php
->authMiddleware([
CheckIsActive::class,
//Authenticate::class,])
```php
->authMiddleware([
CheckIsActive::class,
//Authenticate::class,])
use Filament\Http\Middleware\Authenticate;
class CheckIsActive extends Authenticate
{
protected function authenticate($request, array $guards): void
{
$auth = Filament::auth();
$user = $auth->user();
$panel = Filament::getCurrentPanel();

if (! ($auth->check()
&& $user instanceof FilamentUser
&& $user->canAccessPanel($panel)
&& $user->is_active)) {
Session::flush();
FilamentView::registerRenderHook(
'panels::auth.login.form.before',
fn (): View => view('filament.views.general.hook-before-login')
);
$this->unauthenticated($request, $guards);
}
}

protected function redirectTo($request): ?string
{
return Filament::getLoginUrl();
}
}
use Filament\Http\Middleware\Authenticate;
class CheckIsActive extends Authenticate
{
protected function authenticate($request, array $guards): void
{
$auth = Filament::auth();
$user = $auth->user();
$panel = Filament::getCurrentPanel();

if (! ($auth->check()
&& $user instanceof FilamentUser
&& $user->canAccessPanel($panel)
&& $user->is_active)) {
Session::flush();
FilamentView::registerRenderHook(
'panels::auth.login.form.before',
fn (): View => view('filament.views.general.hook-before-login')
);
$this->unauthenticated($request, $guards);
}
}

protected function redirectTo($request): ?string
{
return Filament::getLoginUrl();
}
}
If user is not active, redirect works but my view for auth.login.form.before hook is not showed What I'm missing ?
2 Replies
yagrasdemonde
yagrasdemondeOP9mo ago
Idea?
LeandroFerreira
LeandroFerreira9mo ago
you could customize the login process
public function panel(Panel $panel): Panel
{
return $panel
->login(Login::class)
...
public function panel(Panel $panel): Panel
{
return $panel
->login(Login::class)
...
class Login extends AuthLogin
{
protected function getEmailFormComponent(): Component
{
return parent::getEmailFormComponent()
->rules([
function () {
return function (string $attribute, $value, Closure $fail) {

$active = User::whereEmail($value)
->whereActive(true)->exists();

if (! $active) {
$fail('The :attribute '.$value.' is not active. Please contact the administrator.');
}
};
},
]);
}
}
class Login extends AuthLogin
{
protected function getEmailFormComponent(): Component
{
return parent::getEmailFormComponent()
->rules([
function () {
return function (string $attribute, $value, Closure $fail) {

$active = User::whereEmail($value)
->whereActive(true)->exists();

if (! $active) {
$fail('The :attribute '.$value.' is not active. Please contact the administrator.');
}
};
},
]);
}
}
Want results from more Discord servers?
Add your server