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
- <?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 functionhandle($request, Closure $next)
- {
- if(Auth::user()->user_type =='Admin')
- {
- return$next($request);
- }
- returnredirect()->guest('/');
- }
- }
<?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
- protected $routeMiddleware=[
- 'auth'=>IlluminateAuthMiddlewareAuthenticate::class,
- 'auth.basic'=>IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
- 'bindings'=>IlluminateRoutingMiddlewareSubstituteBindings::class,
- 'can'=>IlluminateAuthMiddlewareAuthorize::class,
- 'guest'=>AppHttpMiddlewareRedirectIfAuthenticated::class,
- 'throttle'=>IlluminateRoutingMiddlewareThrottleRequests::class,
- 'admin'=>AppHttpMiddlewareAdminMiddleware::class,
- ];
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.
- Route::group(array('prefix'=>'administration','middleware'=>['auth','admin']),function()
- {
- Route::get('dashboard',function(){
- return"Welcome to Administration";
- });
- });
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.
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