title: Laravel Multi Auth - Admin Guard
body: <p>I have two guards in laravel</p>
  
  <pre><code>'guards' =&gt; [
          'web' =&gt; [
              'driver' =&gt; 'session',
              'provider' =&gt; 'users',
          ],
  
          //Our Admin custom driver
          'web_admin' =&gt; [
              'driver' =&gt; 'session',
              'provider' =&gt; 'admins',
          ],
      ],
  </code></pre>
  
  <p>and providers</p>
  
  <pre><code>'providers' =&gt; [
      'users' =&gt; [
          'driver' =&gt; 'eloquent',
          'model' =&gt; App\User::class,
      ],
  
       //Admin user provider
        'admins' =&gt; [
            'driver' =&gt; 'eloquent',  //We are using eloquent model
            'model' =&gt; App\Admin::class,
        ],
  ],
  </code></pre>
  
  <p>The default is </p>
  
  <pre><code>'defaults' =&gt; [
          'guard' =&gt; 'web',
          'passwords' =&gt; 'users',
      ],
  </code></pre>
  
  <p>When i log in as admin and try to access the user profile it asks me to login as user which is normal. But what im looking for is, admin should be able to access whole site as admin login. </p>
  
  <p>The reason i choose multi auth over rbac is because i have 5 types of users and each have different registration fields and login. Each user have a set of tools too.</p>
  
  <p>So i want admin guard to be able to access all guards too.</p>
  
  <p>Business guard to be able to access only users guard.</p>
  
  <p>App/Http/Controllers/AdminAuth/LoginController</p>
  
  <pre><code>&lt;?php
  
  //LoginController.php
  
  namespace App\Http\Controllers\AdminAuth;
  
  use Illuminate\Http\Request;
  use App\Http\Controllers\Controller;
  
  //Class needed for login and Logout logic
  use Illuminate\Foundation\Auth\AuthenticatesUsers;
  
  //Auth facade
  use Auth;
  
  class LoginController extends Controller
  {
      //Where to redirect admin after login.
      protected $redirectTo = '/admin/home';
  
      //Trait
      use AuthenticatesUsers;
  
      //Custom guard for admin
      protected function guard()
      {
        return Auth::guard('web_admin');
      }
  
      //Shows admin login form
     public function showLoginForm()
     {
         return view('admin.auth.login');
     }
  }
  </code></pre>
  
  <p>App/Http/Controllers/Auth/LoginController</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers\Auth;
  use Socialite;
  
  use App\Http\Controllers\Controller;
  use Illuminate\Foundation\Auth\AuthenticatesUsers;
  
  class LoginController extends Controller
  {
      /*
      |--------------------------------------------------------------------------
      | Login Controller
      |--------------------------------------------------------------------------
      |
      | This controller handles authenticating users for the application and
      | redirecting them to your home screen. The controller uses a trait
      | to conveniently provide its functionality to your applications.
      |
      */
  
      use AuthenticatesUsers;
  
      /**
       * Where to redirect users after login.
       *
       * @var string
       */
      protected $redirectTo = '/home';
  
      /**
       * Create a new controller instance.
       *
       * @return void
       */
      public function __construct()
      {
          $this-&gt;middleware('guest')-&gt;except('logout');
      }
  
      /**
       * Redirect the user to the GitHub authentication page.
       *
       * @return \Illuminate\Http\Response
       */
        public function redirectToProvider($social)
        {
            return Socialite::driver($social)-&gt;redirect();
        }
  
        /**
         * Obtain the user information from GitHub.
         *
         * @return \Illuminate\Http\Response
         */
        public function handleProviderCallback($social)
        {
            $user = Socialite::driver($social)-&gt;user();
  
            // $user-&gt;token;
        }
  }
  </code></pre>
  
  <p>Similarly i have created middleware for admin too in App/Https/Middleware/AuthenticateAdmin.php</p>
  
  <pre><code>&lt;?php
  
  //AuthenticateAdmin.php
  
  namespace App\Http\Middleware;
  
  use Closure;
  
  //Auth Facade
  use Auth;
  
  class AuthenticateAdmin
  {
     public function handle($request, Closure $next)
     {
         //If request does not comes from logged in admin
         //then he shall be redirected to admin Login page
         if (! Auth::guard('web_admin')-&gt;check()) {
             return redirect('/admin/login');
         }
  
         return $next($request);
     }
  }
  </code></pre>
  
  <p>And RedirectIfAdminAuthenticated</p>
  
  <pre><code>&lt;?php
  
  //RedirectIfAdminAuthenticated.php
  
  namespace App\Http\Middleware;
  
  use Closure;
  
  //Auth Facade
  use Auth;
  
  class RedirectIfAdminAuthenticated
  {
  
    public function handle($request, Closure $next)
    {
        //If request comes from logged in user, he will
        //be redirect to home page.
        if (Auth::guard()-&gt;check()) {
            return redirect('/home');
        }
  
        //If request comes from logged in admin, he will
        //be redirected to admin's home page.
        if (Auth::guard('web_admin')-&gt;check()) {
            return redirect('/admin/home');
        }
        return $next($request);
    }
  }
  </code></pre>
  
  <p>RedicrectIfAuthenticated</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Middleware;
  
  use Closure;
  use Illuminate\Support\Facades\Auth;
  
  class RedirectIfAuthenticated
  {
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @param  string|null  $guard
       * @return mixed
       */
      public function handle($request, Closure $next, $guard = null)
      {
          if (Auth::guard($guard)-&gt;check()) {
              return redirect('/home');
          }
  
          return $next($request);
      }
  }
  </code></pre>