Laravel 5.4 - Simple and easy solution to create admin middleware to authenticate user type

Laravel 5.4 - Simple and easy solution to create admin middleware to authenticate user type

In this tutorial, I will let you know the easy solution to create middleware to make sure if logged user has the privileges for admin.

I have user table having column name "user_type" that manage the status of the users.

Please follow the steps to handle admin middleware :

Add Middleware

First i will create AdminMiddleware.php in following path app/Http/Middleware/AdminMiddleware.php

  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. class AdminMiddleware
  5. {
  6. /**
  7. * Handle an incoming request. User must be logged in to do admin check
  8. *
  9. * @param \Illuminate\Http\Request $request
  10. * @param \Closure $next
  11. * @return mixed
  12. */
  13. public function handle($request, Closure $next)
  14. {
  15. if (\Auth::user()->user_type == 'Admin')
  16. {
  17. return $next($request);
  18. }
  19. return redirect()->guest('/');
  20. }
  21. }

After creating middleware don't forget to register the middleware as routeMiddleware in app/Http/Kernel.php

app/Http/Kernel.php
  1. protected $routeMiddleware = [
  2. 'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
  3. 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
  4. 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
  5. 'can' => \Illuminate\Auth\Middleware\Authorize::class,
  6. 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
  7. 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
  8. 'admin' => \App\Http\Middleware\AdminMiddleware::class,
  9. ];

Now you have successfully configured the admin middleware.

Ok, let's assign this admin middleware in routes/web.php

Routes

In this step, we will add some routes within the admin middleware to check it is working fine or not.

  1. Route::group(array('prefix'=>'administration','middleware' => ['auth', 'admin']), function ()
  2. {
  3. Route::get('dashboard',function(){
  4.     return "Welcome to Administration";
  5. });
  6. });

If you want to implement multi auth with multi models in Laravel 5.4 then follow the link :

How to implement multi auth in Laravel 5.4 with example

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
sohbet okey oyna
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort