Skip to content

Instantly share code, notes, and snippets.

@CodeWithDennis
Last active February 12, 2025 19:53
Show Gist options
  • Save CodeWithDennis/0ecd53c98fe4375489e821bed9ab9eec to your computer and use it in GitHub Desktop.
Save CodeWithDennis/0ecd53c98fe4375489e821bed9ab9eec to your computer and use it in GitHub Desktop.
<?php
namespace App\Filament\Pages\Auth;
use App\Models\User;
use DiogoGPinto\AuthUIEnhancer\Pages\Auth\Concerns\HasCustomLayout;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Http\Responses\Auth\Contracts\LoginResponse;
use Filament\Pages\Auth\Login as BasePage;
class Login extends BasePage
{
use HasCustomLayout;
private function canAuthenticateAs(): bool
{
return app()->isLocal() && app()->hasDebugModeEnabled() && config('app.authenticate_as') === true;
}
public function authenticate(): ?LoginResponse
{
$authenticateAs = $this->form->getState()['user'] ?? null;
if ($this->canAuthenticateAs() === true && $authenticateAs) {
auth()->login(User::where('email', $authenticateAs)->first());
return app(LoginResponse::class);
}
return parent::authenticate();
}
public function form(Form $form): Form
{
if ($this->canAuthenticateAs() === true) {
$users = User::select(['email', 'name', 'is_owner'])
->withCount('companies')
->get();
return $form->schema([
Select::make('user')
->preload()
->required()
->searchable()
->allowHtml()
->options($users->mapWithKeys(function (User $user) {
return [
$user->email => view('filament.pages.auth.partials.authenticate-as', [
'user' => $user,
])->render(),
];
})),
]);
}
return parent::form($form);
}
public function mount(): void
{
parent::mount();
if (app()->isLocal() && $this->canAuthenticateAs() === false) {
$this->form->fill([
'email' => config('app.demo_user'),
'password' => config('app.demo_password'),
'remember' => true,
]);
}
}
}
// authenticate-as.blade.php
<div class="flex items-center">
<span class="mr-1">{{ $user->name }}</span>
<div class="ml-auto flex gap-1">
@if(! $user->is_owner)
<x-filament::badge color="gray" size="md">
<strong>{{ $user->companies_count }}</strong> {{ Str::plural(__('Company'), $user->companies_count) }}
</x-filament::badge>
@endif
<x-filament::badge color="{{ $user->is_owner ? 'danger' : 'success' }}" size="md">
{{ $user->is_owner ? __('Admin') : __('Tenant') }}
</x-filament::badge>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment