Manual Authentication of a Table Field in the Laravel Framework 5.3

1

I'm now starting with Framewok Laravel 5.3. , however when I create it with artisan o make:auth it automatically creates the views , model , and controllers for Login . Only I added a field in tables users called status of type boolean . I need to check if this status is as true (1) or false (0) .

How I would do this procedure. So, I thought, I create an example object:

$data = Auth::user();

and would check with if

if($data->status === true){

he would join the session

} else {

he would return to the login with a message saying that his status is currently disabled.

}

Because if you do not have this check, the user will simply enter the admin panel. I hope you have understood.

    
asked by anonymous 21.12.2016 / 00:12

1 answer

1

Create a middleware and set up your project on the administration routes to be checked whether or not you can, follow the step by step:

At the console type:

  

php artisan make:middleware CheckStatus

a app/Http/Middleware file will be created in the CheckStatus.php folder as follows:

<?php

namespace App\Http\Middleware;

use Closure;

class CheckStatus
{        
    public function handle($request, Closure $next)
    {
        if (\Auth::user()->status == 1) 
        {
            return redirect('home');
        }    
        return $next($request);
    }

}

to register this middleware created CheckStatus between app/Http/Kernel.php in $routeMiddleware add a key (< strong> auth.status ) as follows:

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

    'auth.status' => \App\Http\Middleware\CheckStatus::class,
];

Then add in your route (s) ( Route ):

On a route:

Route::get('admin/', function () 
{

})->middleware('auth.status');

In a route group:

Route::group(['middleware' => 'auth.status'], function () {
    Route::get('/admin/change', function ()    
    {

    });

    Route::get('admin/profile', function () 
    {

    });
});

It can also be added directly in Controller :

class AdminController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth.status');

    }
}

In this link has the translation made by Artisans group

References:

21.12.2016 / 00:33