# How to Disable Users from Login in Laravel

Laravel 8 does not include the auth system by default now, You can Read [Laravel 8 Authentication](https://dev.to/techtoolindia/laravel-8-authentication-n40).

The common feature of ban or suspend any user from authentication is major missing in Laravel. 

Here is What the login form Will looks like after banning any user from the application.
![Suspended User](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5nnru53lge41sa8tzya.png)

# Steps To Achieve the outcome

##Step 1 - Add New Column 'status' in the users table

Create a migration by running the command below

```php
php artisan make:migration add_status_to_users_table
```
After Migration File is created update the following code in the up() function.

```php
Schema::table('users', function (Blueprint $table) {
    $table->integer('status')->default(1);
});
```
Add 'status' in Fillable in app\Models\User.php

```php
protected $fillable = [
        'name',
        'email',
        'password',
        'status'
];
```

##Step 2 - Create a Middleware - CheckBanned

Create a middleware by running the command below.

```php
php artisan make:middleware CheckBanned
```

Replace the handle method in app/Http/CheckBanned.php

```php
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

IN 'web' Middleware group register the CheckBanned Middleware by putting the code below.

```php
\App\Http\Middleware\CheckBanned::class,
```
##Step 4 - Display The Error on the log-in page.

Open login blade 'resources/views/auth/login.blade.php'

Add The following code to display the error message.

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

You can watch the video for complete Explanation and implementation.

%[https://www.youtube.com/watch?v=MY6BcDW8zwo]

Thank You for Reading

Reach Out To me.
[Twitter](https://twitter.com/techtoolindia)
[Instagram](https://www.instagram.com/techtoolindia/)
[TechToolIndia](https://youtube.com/techtoolindia)
