Monday, 8 October 2018

How to check status of user before login?

Create a middlewear named CheckStatus

and add the below lines.

<?php
namespace App\Http\Middleware;

use Closure;
use Auth;
use Session;

class CheckStatus
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        if(Auth::check() && Auth::user()->status != '1'){
            Auth::logout();
            $request->session()->flash('alert-danger', 'Your Account is deactivated.');
            return redirect('/login')->with('erro_login', 'Your error text');
        }
        return $response;
    }
}

Now Open the kernal.php.
Find  protected $routeMiddleware and add below line.

'checkstatus' => \App\Http\Middleware\CheckStatus::class,

Next: In loginController contructor should be updated as below.

public function __construct()  { $this->middleware(['guest','checkstatus'])->except('logout');  }

Next: In  your login.blade.php the error should be there.


<div class="flash-message"> @foreach (['danger', 'warning', 'success', 'info'] as $msg)
            @if(Session::has('alert-' . $msg))
            <p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></p>
            @endif
            @endforeach </div>

Note. In your user table status field with int(1) should be there.

No comments:

Machine Learning - Potato Leaf Disease Prediction

Step 1: import numpy as np import pandas as pd import splitfolders import matplotlib.pyplot as plt import tensorflow as tf from tensorflow i...