Raviya Technical
Published in

Raviya Technical

How to Disable Users from Login in Laravel

Step 1 — Add New Column ‘status’ in the users table

php artisan make:migration add_status_to_users_table
Schema::table('users', function (Blueprint $table) {
$table->integer('status')->default(1);
});
protected $fillable = [
'name',
'email',
'password',
'status'
];

Step 2 — Create a Middleware — CheckBanned

php artisan make:middleware CheckBanned
public function handle(Request $request, Closure $next)
{
if(auth()->check() && (auth()->user()->status == 0)){
Auth::logout();

$request->session()->invalidate();

$request->session()->regenerateToken();

return redirect()->route('login')->with('error', 'Your Account is suspended, please contact Admin.');

}

return $next($request);
}

Step 3 — Register the Middleware — app/Http/Kernel.php

\App\Http\Middleware\CheckBanned::class,

Step 4 — Display The Error on the log-in page.

@if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
@endif

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store