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 post we will give you information about Laravel 5.4 – Simple and easy solution to create admin middleware to authenticate user type. Hear we will give you detail about Laravel 5.4 – Simple and easy solution to create admin middleware to authenticate user typeAnd how to use it also give you demo for it if it is necessary.

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 AppHttpMiddleware;
  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 IlluminateHttpRequest $request
  10. * @param Closure $next
  11. * @return mixed
  12. */
  13. public functionhandle($request, Closure $next)
  14. {
  15. if(Auth::user()->user_type =='Admin')
  16. {
  17. return$next($request);
  18. }
  19. returnredirect()->guest('/');
  20. }
  21. }
<?php

namespace AppHttpMiddleware;

use Closure;

class AdminMiddleware
{

    /**
     * Handle an incoming request. User must be logged in to do admin check
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::user()->user_type == 'Admin')
        {
            return $next($request);
        }

        return redirect()->guest('/');
    }
}

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'=>IlluminateAuthMiddlewareAuthenticate::class,
  3. 'auth.basic'=>IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
  4. 'bindings'=>IlluminateRoutingMiddlewareSubstituteBindings::class,
  5. 'can'=>IlluminateAuthMiddlewareAuthorize::class,
  6. 'guest'=>AppHttpMiddlewareRedirectIfAuthenticated::class,
  7. 'throttle'=>IlluminateRoutingMiddlewareThrottleRequests::class,
  8. 'admin'=>AppHttpMiddlewareAdminMiddleware::class,
  9. ];
protected $routeMiddleware = [
        'auth' => IlluminateAuthMiddlewareAuthenticate::class,
        'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
        'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class,
        'can' => IlluminateAuthMiddlewareAuthorize::class,
        'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,
        'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,
        'admin' => AppHttpMiddlewareAdminMiddleware::class,
    ];

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. });
Route::group(array('prefix'=>'administration','middleware' => ['auth', 'admin']), function ()
  {
     Route::get('dashboard',function(){
     	return "Welcome to Administration";
     });
  });


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

Try this code and feel free to give your suggestions.

Label :

PHP

Laravel PHP Framework

How To

MVC

Web Development

Hope this code and post will helped you for implement Laravel 5.4 – Simple and easy solution to create admin middleware to authenticate user type. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  15  =  20

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US