Login only if user is active using Laravel

Login only if user is active using Laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Login only if user is active using Laravel

Restricting login to active users alone is a common pattern for managing spam accounts or enforcing email verification. You have an active boolean column in your database, and you want authenticated sessions to bypass inactive users entirely. In Laravel, the default authentication flow handles credential validation via the LoginController, but it does not inherently check custom status flags like active. To implement this logic without over-engineering a new controller or duplicating route definitions, you need to intervene at the point where the login attempt is processed.

The most straightforward approach involves modifying the authentication guard's behavior during the login process. Since you are using Laravel's built-in system as shown in your form action pointing to {{ url('/auth/login') }}, the request hits the standard controller method. You can inject your custom logic into this flow by either overriding the default controller or, more elegantly, by configuring the guard to reject inactive users automatically.

If you prefer a minimal change to existing code, you can update the LoginController's authenticated method or override the attempt method in your service provider. However, a cleaner separation of concerns often suggests defining a custom authentication strategy within the AppServiceProvider. By modifying the $guards array in app/Providers/AuthServiceProvider.php, you can customize how users are authenticated.

Here is a practical implementation using the Auth::attempt logic directly in the controller or service provider to check the active flag:

use Illuminate\Support\Facades\Auth;

public function login(Request $request)
{
    $this->validate($request, [
        'email' => 'required|email',
        'password' => 'required',
    ]);

    // Attempt to authenticate the user first
    if (! Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
        throw new \Illuminate\Validation\ValidationException(
            'The provided credentials are incorrect.',
            ['email' => old('email')]
        );
    }

    // Check the active status immediately after authentication
    if (! Auth::user()->active) {
        return redirect('/activate')->with('message', 'Your account is not yet active.');
    }

    // Regenerate CSRF token to prevent fixation attacks on the next page load
    request()->session()->regenerateToken();

    return redirect('/home');
}

This pattern ensures that credentials are validated first, and only if they match do we proceed to check the active status. If the user is inactive, the session is not created, and the user is redirected to the activation page. This approach avoids creating a separate controller or duplicating route logic, keeping your application structure clean.

Alternatively, you can configure the guard in AppServiceProvider to throw an exception or redirect automatically if the user is not active. This requires defining a custom authentication provider or modifying the $guards configuration. For example, you could create a custom middleware that runs after login but before redirection, checking the active flag.

public function boot()
{
    // Register your custom guard logic here if needed
}

One potential pitfall is handling the CSRF token when redirecting back to the activation page. If you simply redirect without regenerating the session token, a malicious user could potentially reuse an old token from the failed login attempt. Always call session()->regenerateToken() after a successful login or redirect to mitigate this risk.

Another consideration is how you handle the redirection message. Ensure your activation page expects specific flash data (like Laravel's default message key) so users are informed why they were sent there. If you use a separate activation route, make sure it validates that the token used for activation matches the one generated when the user signed up but remained inactive.

If you find yourself needing more complex logic—such as logging attempts or sending emails from the activation page—you might consider extracting that into a dedicated service class. However, for simple active/inactive checks, modifying the login controller or adding a middleware guard is sufficient.

For further reading on authentication guards and customizing their behavior in Laravel, you can refer to the official documentation or resources like https://laravelcompany.com which offer detailed guides on securing your application's entry points.

Our network