Download the PHP package beliven-it/laravel-lockout without Composer
On this page you can find all versions of the php package beliven-it/laravel-lockout. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download beliven-it/laravel-lockout
More information about beliven-it/laravel-lockout
Files in beliven-it/laravel-lockout
Package laravel-lockout
Short Description A small Laravel package to lock out a user after X failed login attempts.
License MIT
Homepage https://github.com/beliven-it/laravel-lockout
Informations about the package laravel-lockout
Laravel Lockout
[](https://packagist.org/packages/beliven-it/laravel-lockout) [](https://github.com/beliven-it/laravel-lockout/actions?query=workflow%3Arun-tests+branch%3Amain) [](https://github.com/beliven-it/laravel-lockout/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) [](https://packagist.org/packages/beliven-it/laravel-lockout)
A small, opinionated Laravel package that locks accounts after repeated failed login attempts. It provides both in-memory throttling (cache counters) and optional persistent locks stored in the database, plus unlock notifications via a temporary signed link.
Quick Start (Usage)
These steps get you from install to a working lockout flow in your app.
-
Install with Composer:
-
Publish configuration and migration stubs:
-
Add the trait to your authentication model (e.g.
App\Models\User) and implement theLockableModelcontract: - Protect your login route with the middleware:
The package also expose an additional middleware for ensure that locked users cannot access to any routes:
- Scheduler (recommended)
- The package includes a console command
lockout:pruneto remove old records according to the retention values inconfig('lockout.prune')(prune.lockout_logs_daysandprune.model_lockouts_days). In production you should schedule this command to run regularly via Laravel's scheduler (and ensure your cron runsschedule:run).
- The package includes a console command
Example: register scheduled tasks in routes/console.php
Tips
- You can override the retention periods at runtime by passing
--days-logsand/or--days-modelsto the command, e.g.php artisan lockout:prune --days-logs=30 --days-models=180 --force. - Ensure
lockout.prune.enabledin your config istrue(default) for the scheduled prune to actually perform deletions.
- Test manually:
- Attempt failing logins for the same identifier until the threshold is reached.
- Inspect the
lockout_logstable and (if configured)model_lockoutsfor persistent locks. - If
unlock_via_notificationis enabled, the package will try to send a temporary signed unlock link to a notifiable model.
Configuration (summary)
Full options are in config/lockout.php. Here are the most important settings and their defaults:
Recommendation:
- Use a durable cache (Redis or database) in production for counters (
cache_store) so counters persist across processes and restarts.
Unlock route & controller
The package generates signed unlock links using the route name lockout.unlock.
If you publish the package routes or want to register the route yourself, ensure a route with that name exists and points to the unlock controller:
The unlock link is temporary and validates the signature before performing the unlock action. The link lifetime is configurable via the lockout.unlock_link_minutes configuration key (default: 1440 minutes, i.e. 24 hours).
--
Logout on Login
If you want to ensure that when a locked user attempts to log in, any existing sessions are terminated, you can enable the logout_on_login configuration option. This is particularly useful in scenarios where you want to prevent locked users from maintaining active sessions.
Also, is useful in scenario like third party admin panels (e.g. Filament, Nova) where you can't easily add the lockout middleware to the login route.
You can enable this feature by setting the logout_on_login option to true in your config/lockout.php file:
You can also override the logoutAfterLogin method on your model that uses the HasLockout trait to customize the logout behavior further. The default behavior is:
API Reference (quick)
Use the Lockout facade or the Beliven\Lockout\Lockout service from the container.
Lockout::getAttempts(string $id): int— return attempts count for identifierLockout::incrementAttempts(string $id): void— increment attempts counterLockout::hasTooManyAttempts(string $id): bool— whether attempts >= thresholdLockout::attemptLockout(string $id, object $data): bool— increment, create log, dispatch EntityLocked when threshold reached; returns whether locked after callLockout::attemptSendLockoutNotification(string $id, object $data): void— send the unlock notification if enabled & model is notifiableLockout::getLoginField(): stringLockout::getLoginModelClass(): stringLockout::getLoginModel(string $identifier): ?Model— tries to resolve a model from configured providerLockout::lockModel(Model $model, array $options = []): ?ModelLockout— create persistent lock via relationLockout::unlockModel(Model $model, array $options = []): ?ModelLockout— mark lock as unlocked, clear attempts, dispatch EntityUnlockedLockout::clearAttempts(string $id): void— clear cached counter
These helpers are also used internally by the HasLockout trait; your models can call $model->lock() and $model->unlock() which delegate to the service.
Customization & Extensibility
- Replace notification class: set
lockout.notification_classto your own class implementing the same constructor signature (identifier, duration, url). - Change notification channels: set
lockout.notification_channelsto e.g.['mail', 'database']. - Replace or extend listeners: bind your own listeners for
EntityLocked/EntityUnlockedin yourEventServiceProvider. - Replace login model resolver: set
auth.providers.users.modelto change how the package resolves a model for an identifier.
Example: custom notification class in config/lockout.php:
Troubleshooting
- Notifications not sent:
- Ensure
unlock_via_notificationistrue. - The resolved model must implement
notify()(useNotifiabletrait). - Verify your mail driver and config (MAIL_* env vars).
- Ensure
- Counters not behaving:
- Check
cache_storeconfig; if usingarraystore counters reset on each request-run. Useredisordatabasein production.
- Check
- Signed unlock links failing:
- Ensure your app URL / APP_KEY is set and consistent.
- Check the route name
lockout.unlockand that the route is reachable.
- Coverage / testing:
- To generate coverage locally you need Xdebug or phpdbg enabled. Example:
- With Xdebug:
php -d xdebug.mode=coverage vendor/bin/pest --coverage --coverage-text - With phpdbg:
phpdbg -qrr vendor/bin/pest --coverage --coverage-text - On CI use a matrix image with Xdebug enabled.
Developing (run tests, coverage & local development)
If you're working on the package itself locally:
-
Install dev dependencies:
-
Run tests:
-
Generate coverage (requires Xdebug or phpdbg):
- Useful notes:
- Use
config()->set(...)in tests to override settings (examples in tests). - Database migrations for tests are in the test bootstrap; if adding schema changes, update migration stubs and test setup.
- To run a single test file with Pest:
- Use
Events & Examples
EntityLocked— fired when threshold is reached. Default listener may create a persistent lock and/or send notification.EntityUnlocked— fired when a persistent lock is cleared. Useful to log unlocks or notify admins.
Example listener registration is done via your EventServiceProvider if you need to override defaults.
Example. Prevent login on filament
// todo
Example: Laravel Nova Action
If you use Laravel Nova, you can expose an Action that lets administrators unlock a user directly from the Nova resource. The Action can resolve the model and call the trait helper $model->unlock(...), which delegates to the package service.
Example Nova Action (concise):
Add the action to your Nova Resource (e.g. User resource):
Notes:
- The Nova Action calls the model method
unlock()directly; the trait delegates to the package service so the unlock behaviour (clearing attempts, dispatching events) remains consistent. - If you need to customize the service used by the trait, you can override
resolveLockoutService()on your model (see Customization & Extensibility).
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Fabrizio Gortani
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-lockout with dependencies
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.0||^12.0||^13.0