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
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort