To let only active users log in, add an is_active flag to the users table, include it in the credentials Laravel checks when attempting authentication, and add a middleware that signs out users who are deactivated while they still hold a session. The examples below use Laravel 11, 12 and 13 with PHP 8.2 or later; the differences for Laravel 10 and earlier are noted.
1. Add the is_active column
php artisan make:migration add_is_active_to_users_table --table=users
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('is_active')->default(false)->after('password');
});
}
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('is_active');
});
}
};
New users start inactive until your activation flow (email confirmation, admin approval, etc.) sets is_active to true.
2. Include the flag in the authentication attempt
Authentication must always run as a POST request. A login implemented as Route::get() leaks credentials into URLs, browser history and server logs, and can be triggered by prefetchers and crawlers.
The clean way to restrict login is to add the flag to the credentials array, so the normal Auth::attempt() flow does the work and the failure looks like any other invalid login.
With the LoginController from Laravel UI or Breeze:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class LoginController extends Controller
{
protected function credentials(Request $request)
{
return array_merge(
$request->only($this->username(), 'password'),
['is_active' => true],
);
}
}
$request is type-hinted as a method parameter, so Laravel’s container injects it. Capturing $request with use ($request) in a route closure that never received it is a bug: the variable is undefined at that point.
With Laravel Fortify, do the same in FortifyServiceProvider::boot():
<?php
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Fortify;
Fortify::authenticateUsing(function (Request $request) {
$user = User::where('email', $request->string('email'))->first();
if ($user && $user->is_active && Hash::check($request->password, $user->password)) {
return $user;
}
return null;
});
3. Sign out users deactivated mid-session
The credentials check only runs at login. To cover users who are deactivated while logged in, add a middleware to your protected routes:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;
class EnsureUserIsActive
{
public function handle(Request $request, Closure $next): Response
{
if ($request->user() && ! $request->user()->is_active) {
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect()->route('login')
->withErrors(['email' => 'Your account has been deactivated.']);
}
return $next($request);
}
}
Register the alias in bootstrap/app.php on Laravel 11:
<?php
use App\Http\Middleware\EnsureUserIsActive;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(web: __DIR__ . '/../routes/web.php')
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'active' => EnsureUserIsActive::class,
]);
})
->create();
On Laravel 10 and earlier, add it to $middlewareAliases in app/Http/Kernel.php instead (the property was called $routeMiddleware before Laravel 10). Then protect routes in routes/web.php:
<?php
use Illuminate\Support\Facades\Route;
Route::middleware(['auth', 'active'])->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
});
4. Cover it with a feature test
<?php
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
test('inactive users cannot log in', function () {
$user = User::factory()->create(['is_active' => false]);
$this->post('/login', ['email' => $user->email, 'password' => 'password'])
->assertSessionHasErrors();
$this->assertGuest();
});
test('active users can log in', function () {
$user = User::factory()->create(['is_active' => true]);
$this->post('/login', ['email' => $user->email, 'password' => 'password'])
->assertRedirect();
$this->assertAuthenticatedAs($user);
});
(The Pest syntax above needs Pest installed; with plain PHPUnit use the same calls inside public function test_...() methods.)
Common mistakes to avoid
- Authenticating in a GET route. Login attempts belong in POST handlers only.
use ($request)in a closure that never received$request. Type-hintRequest $requestas the closure or controller method parameter instead.- Checking activation only at login. A user deactivated after logging in keeps a working session until the middleware in step 3 runs.
- Telling the user why they failed. Returning a generic “these credentials do not match” for inactive accounts avoids leaking which emails have accounts.