Created
January 15, 2024 14:10
-
-
Save billydekid/60995185ec281cc5e181391b3113a4a2 to your computer and use it in GitHub Desktop.
Filament PHP login with LdapRecord
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Providers\Filament; | |
| use Filament\Pages; | |
| use Filament\Panel; | |
| use Filament\Widgets; | |
| use Filament\PanelProvider; | |
| use Filament\Support\Colors\Color; | |
| use Filament\Http\Middleware\Authenticate; | |
| use Jeffgreco13\FilamentBreezy\BreezyCore; | |
| use Illuminate\Session\Middleware\StartSession; | |
| use Illuminate\Cookie\Middleware\EncryptCookies; | |
| use Illuminate\Routing\Middleware\SubstituteBindings; | |
| use Illuminate\Session\Middleware\AuthenticateSession; | |
| use Illuminate\View\Middleware\ShareErrorsFromSession; | |
| use Filament\Http\Middleware\DisableBladeIconComponents; | |
| use Filament\Http\Middleware\DispatchServingFilamentEvent; | |
| use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken; | |
| use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse; | |
| use App\Filament\Auth\Login; | |
| class AdminPanelProvider extends PanelProvider | |
| { | |
| public function panel(Panel $panel): Panel | |
| { | |
| return $panel | |
| ->default() | |
| ->id('admin') | |
| ->path('admin') | |
| ->login(Login::class) | |
| ->colors([ | |
| 'primary' => Color::Amber, | |
| ]) | |
| ->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources') | |
| ->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages') | |
| ->pages([ | |
| Pages\Dashboard::class, | |
| ]) | |
| ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets') | |
| ->widgets([ | |
| Widgets\AccountWidget::class, | |
| Widgets\FilamentInfoWidget::class, | |
| ]) | |
| ->middleware([ | |
| EncryptCookies::class, | |
| AddQueuedCookiesToResponse::class, | |
| StartSession::class, | |
| AuthenticateSession::class, | |
| ShareErrorsFromSession::class, | |
| VerifyCsrfToken::class, | |
| SubstituteBindings::class, | |
| DisableBladeIconComponents::class, | |
| DispatchServingFilamentEvent::class, | |
| ]) | |
| ->authMiddleware([ | |
| Authenticate::class, | |
| ]); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Filament\Auth; | |
| use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException; | |
| use Filament\Facades\Filament; | |
| use Filament\Forms\Form; | |
| use Filament\Forms\Components\TextInput; | |
| use Filament\Forms\Components\Component; | |
| use Filament\Http\Responses\Auth\Contracts\LoginResponse; | |
| use Filament\Notifications\Notification; | |
| use Filament\Pages\Auth\Login as BaseAuth; | |
| use Illuminate\Validation\ValidationException; | |
| class Login extends BaseAuth | |
| { | |
| public function form(Form $form): Form | |
| { | |
| return $form | |
| ->schema([ | |
| $this->getEmailFormComponent(), | |
| $this->getPasswordFormComponent(), | |
| $this->getRememberFormComponent(), | |
| ]) | |
| ->statePath('data'); | |
| } | |
| protected function getCredentialsFromFormData(array $data): array | |
| { | |
| return [ | |
| 'mail' => $data['email'], | |
| 'password' => $data['password'], | |
| ]; | |
| } | |
| public function authenticate(): ?LoginResponse | |
| { | |
| try { | |
| $this->rateLimit(5); | |
| } catch (TooManyRequestsException $exception) { | |
| $this->addError('email', __('filament::login.messages.throttled', [ | |
| 'seconds' => $exception->secondsUntilAvailable, | |
| 'minutes' => ceil($exception->secondsUntilAvailable / 60), | |
| ])); | |
| return null; | |
| } | |
| $data = $this->form->getState(); | |
| if (!Filament::auth()->attempt($this->getCredentialsFromFormData($data), $data['remember'])) { | |
| $this->addError('email', __('filament::login.messages.failed')); | |
| return null; | |
| } | |
| return app(LoginResponse::class); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment