How can we block any user account in Laravel 8

How can we block any user account in Laravel 8

How can we block any user account in Laravel 8

In this tutorial I am going to show how an user account using laravel middleware.

If an user is banned or blocked it will redirect back to login page with an error message.

Step 1

Install Laravel Version8

First of all install a fresh laravel project, download and install Laravel 8 using the below command

composer create-project --prefer-dist laravel/laravel larablockuser

Step 2

Configure Database In .env file

Now, lets create a MySQL database and connect it with laravel project. After creating database ,set database credential in application’s .env file.

    
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3309
    DB_DATABASE=larablockuser  
    DB_USERNAME=root
    DB_PASSWORD=
    

Step 3

Now, use the below command to create default laravel tables automatically.

php artisan migrate

Step 4

app/User.php
    
    class User extends Authenticatable{
        protected $fillable = [
            'name', 'email', 'password', 'blocked_until'  
        ];
    
        protected $dates = [
            'blocked_until'
        ];
    }
    

Step 5

Create Laravel Middleware CheckBlockedUser

php artisan make:middleware CheckBlockedUser

app/http/middleware/CheckBlockedUser.php
    
    class CheckBLockedUser{
        public function handle($request, Closure $next)
        {
            if (auth()->check() && auth()->user()->blocked_until && now()->lessThan(auth()->user()->blocked_until)) {
            $blocked_days = now()->diffInDays(auth()->user()->blocked_date); 
            $message = 'Your account has been suspended for '.$blocked_days.' '.str_plural('day', $blocked_days).'. Please contact administrator.';    
            auth()->logout();     
            return redirect()->route('login')->withMessage($message); 
            }
    
            return $next($request);
        }
    }
    

Step 6

Register Laravel MiddlewStep 5-are

Open app/Http/Kernel.php file and register CheckBlocked middleware here, like below –
    
    protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        
        // ... other middleware classes
    
        \App\Http\Middleware\CheckBlocked::class,
    ],

    

Step 7

Add Error Message

app/resources/views/auth/login.blade.php file and add error message on above of login form body, like below –
    
    ...
        <div class="card-body">
            @if (session('message'))
                <div class="alert alert-danger">{{ session('message') }}</div>   
            @endif
            <form method="POST" action="{{ route('login') }}">
        </div>
    ...

    

php artisan serve

Hit the given url:-

http://localhost:8000/login

Output will be like this:-

login form
I Hope you enjoy this blog

Thank You

Want a Team that Delivers Result ? Connect now with us.

Our Offices

INDIA

F-429, Phase 8B, Industrial Area, SAS Nagar, Punjab 160059

+91 82198-18163

USA

13506 Summerport Village Pky Suite 355 Windermere, FL 34786

+1 (321) 900-0079

CANADA

15 Meltwater Cres, Brampton L6P3V8

+1 (647) 892-6147