Skip to content

Instantly share code, notes, and snippets.

@rajankur
Created October 6, 2016 02:34
Show Gist options
  • Save rajankur/2f26bca717cac84cdfb32fce8d95856b to your computer and use it in GitHub Desktop.
Save rajankur/2f26bca717cac84cdfb32fce8d95856b to your computer and use it in GitHub Desktop.
Setting up admin authentication in Laravel

Setting up admin authentication in Laravel

  • To setup admin authentication, we need to simply create a middleware and use it in your routes.
  • Create a middleware using artisan
php artisan make:middleware Admin
  • This will create a new file called "Admin.php" in app/Http/middleware.
  • Add following code in handle method
$user = $request->user();
if(\Auth::guest())
    return redirect(route('admin_login'));
if($user && $user->user_type != 'a')
    return redirect(route('home'));
return $next($request);
  • Almost, there. Add your new middlware to app/Http/Kernel.php
protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'admin' => \App\Http\Middleware\Admin::class, // Add this line
    ];
  • Notice, user_type will check if the user is an admin or not.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment