1. Go to this page and download the library: Download beliven-it/laravel-lockout library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
beliven-it / laravel-lockout example snippets
use Beliven\Lockout\Traits\HasLockout;
use Beliven\Lockout\Contracts\LockableModel;
class User extends Authenticatable implements LockableModel
{
use HasLockout;
}
use Beliven\Lockout\Http\Middleware\EnsureUserIsNotLocked;
Route::post('/login', [LoginController::class, 'login'])
->middleware(EnsureUserIsNotLocked::class);
use Beliven\Lockout\Http\Middleware\EnsureUserCannotAccessWhenLocked;
Route::middleware([EnsureUserCannotAccessWhenLocked::class])->group(function () {
// protected routes for authenticated users
});
//....
// routes/console.php
use Illuminate\Support\Facades\Schedule;
Schedule::command('lockout:prune --force')->dailyAt('02:00')->withoutOverlapping();
// Optional: separate schedules for logs/models, or different cadences.
Schedule::command('lockout:prune --only-logs --force')->dailyAt('03:00')->withoutOverlapping();
Schedule::command('lockout:prune --only-model --force')->weekly()->withoutOverlapping();
use Beliven\Lockout\Http\Controllers\UnlockController;
Route::get('/lockout/unlock', UnlockController::class)->name('lockout.unlock');
'logout_on_login' => true,
public function logoutOnLockout(?string $guard = null): bool
{
// Default common case behavior
// (to be overridden in concrete models if needed)
Auth::guard($guard)->logout();
session()->invalidate();
session()->regenerateToken();
return true;
}
namespace App\Nova\Actions;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
class UnlockUser extends Action implements ShouldQueue
{
use Queueable;
public function name()
{
return 'Unlock account';
}
/**
* Handle the action for the given models.
*
* @param \Laravel\Nova\Fields\ActionFields $fields
* @param \Illuminate\Support\Collection $models
*/
public function handle(ActionFields $fields, $models)
{
foreach ($models as $model) {
// call the HasLockout trait helper which delegates to the Lockout service
// you can pass optional metadata like reason/actor/requestData
$model->unlock([
'reason' => 'Unlocked by admin via Nova',
'actor' => auth()->user()?->id ?? null,
]);
}
return Action::message('Selected accounts have been unlocked.');
}
public function fields()
{
return [];
}
}
// in app/Nova/User.php
public function actions(Request $request)
{
return [
new \App\Nova\Actions\UnlockUser,
];
}