How afsakar/filament-otp-login works
Project Type and Purpose
• Type: A Laravel-based, open-source FilamentPHP plugin (package).
• Purpose: Replace Filament’s standard username/password login with a One-Time-Password flow—generate, store, send, verify OTP codes.
File Structure
Top-level:
• composer.json / package.json / package tooling (PSR, PHPStan, Pint, PostCSS/Tailwind)
• config/filament-otp-login.php (package config)
• database/migrations/… (migration stub for otp_codes table)
• resources/lang/{en, tr} (translations)
• resources/views (Blade templates for login page & OTP input)
• src/
– FilamentOtpLoginPlugin.php (plugin entry point)
– FilamentOtpLoginServiceProvider.php (Laravel service provider)
– Filament/Forms/OtpInput.php (Form component)
– Filament/Pages/Login.php (OTP-based login page)
– Models/OtpCode.php (Eloquent model) & Contracts/CanLoginDirectly.php
– Notifications/SendOtpCode.php (Laravel Notification)
– Commands/PruneOtpCodes.php (Artisan command to prune old codes)
– Testing/TestsFilamentOtpLogin.php (package tests)
• tests/ (unit and architecture tests)
Extract Additional Insights from README
• Installation: vendor:publish migrations, config, translations, views
• Configurable code length and expiry
• Uses Laravel Notifications (email/SMS channel)
• Cron-driven pruning recommended for production
• Extensible: implement CanLoginDirectly or override login page
Main Components & Their Interactions
A. Laravel Host Application
B. Filament Admin Panel
C. FilamentOtpLoginPlugin
– Registers ServiceProvider hooks
– Publishes migration, config, translations, views
– Binds custom login page and form component
– Hooks into Filament’s auth flow
D. OTPCode Model & Database Table (“otp_codes”)
E. Notification Subsystem
– SendOtpCode notification (email/SMS via Laravel Notification channels)
F. Artisan Command (PruneOtpCodes)
– Scheduled via cron to delete expired codes
G. Config & Localization (config file + lang folders)
H. Frontend Assets (Blade views, TailwindCSS, Alpine.js for OTP input)
Interactions (flow):
1. User → Filament login URL
2. Filament → Plugin → overrides default login page → shows OTP‐Input view
3. User enters email/username → submits form
4. Plugin generates OTP (random of configured length, expiration) → saves to otp_codes table
5. Plugin triggers SendOtpCode notification → Laravel Notification → Email/SMS Gateway
6. User receives OTP → enters code → Plugin verifies against OTPCode model (expires check)
7. On success: plugin calls Filament’s auth guard → issues session / JWT → user logged in
8. Cron job: PruneOtpCodes command → purges expired codes
Architectural Patterns & Design Principles
• Plugin Pattern: encapsulates all OTP logic behind a Filament plugin interface
• MVC:
– Models: OtpCode
– Views: Blade templates (login.blade.php, otp-input.blade.php)
– Controllers/Pages: Filament\Pages\Login
• Service Provider: Laravel’s extension mechanism
• Event-Driven / Notification: decoupled sending of OTP codes
• Scheduled Jobs: Artisan command for data hygiene
• Configuration-Driven: config file drives table name, code length, expiry
• Extensibility:
– Contract CanLoginDirectly for bypass logic
– loginPage() override for custom UI
Technologies & Frameworks to Annotate
• PHP 8+, Laravel framework
• FilamentPHP (Admin panel)
• Eloquent ORM (Models & Migrations)
• Laravel Notifications (Mail/SMS channels)
• Laravel Artisan Commands & Scheduler
• Blade templating, TailwindCSS, Alpine.js for UI
• PHPStan, Pest/PHPUnit for tests
• Composer / NPM for package management
Essential Elements
• Clear labels: component names, data stores, external systems
• Config & Translations: note that they drive plugin behavior
• Extensibility points: CanLoginDirectly contract, custom loginPage override