Laravel Auth Check Guard in template, controllers, and routes

Firstly, make sure you're not missing out: you'll probably want to use Laravel Authorization Policies rather than guard checks.

Remember: Put guards as close to the routes as possible, don't waste time having PHP build the page only to have the template block access. Start with the routes, and work your way out toward templates for tiny detail. Read Laravel Authorization Policies to make sure this isn't more what you need, and debugging Laravel Policies for some strategies to debug them. Back to guard checking.

Example Guard Checking

This example assumes you want to check between a 'user' guard and the 'web' guard. The 'web' guard is the name of the build-in guard that comes with Laravel. You can create custom guards to allow for native multi authentication. See notes on Laravel Native Authentication.

Recap- What are the guard names and where are they defined?

It's important to know the names of the guards which are defines, else how do you check for them?

The guard names are defined in the guards array in your config/auth.php file. By default there's just one , the 'web' guard. The indexes of this array are the names of the various guards your application has got defined.

Note: If you need to create new guards, there's more to it than just adding to this array See notes on Laravel Native Authentication and the Authentication Docs.

Checking current Laravel Guard in the template

For example you might have a template file: home.blade.php and you want to check the user or web guard:

Checking one guard
@if (Auth::guard('user')->check()
  <p>Current visitor is logged on using the user guard.</p>
@endif
Checking multiple guards
@if (Auth::guard('user')->check() || Auth::guard('web')->check())
 <p>Current visitor is either logged in via user guard or web guard.</p>
@endif

Checking current Laravel Guard in the controller

You can also check the guard used in your controllers, for example use: Auth::guard('user')->check()

use Auth;
...
class abc {
  public function index() {
      if (Auth::guard('user')->check())
      {
         echo "Logged in via manager guard
      }
  }
}

Make sure you remember to include the use Auth statement.

Checking the guard in routes using middleware

In your routes file (web.php or api.php) you can apply a guard to entire groups of routes or individual routes.

For example:

Route::group(['prefix' => 'dashboard', 'middleware' => 'auth:web'], function()
{
    Route::get('/weather', 'WeatherController');
}

The above makes the routes within the /dashboard path first pass throuh the authentication middleware which checks for the 'web' guard.

You can check for multiple guards in the auth middleware as follows:

Route::group(['prefix' => 'dashboard', 'middleware' => 'auth:web,user'], function()
{
    Route::get('/weather', 'WeatherController');
}

Notice the use of 'middleware' => 'auth:web,user' passing two guards (web, and user).

Heads up: Don't put spaces in between your guards. 'middleware' => 'auth:web, user' will not work!

Collect recurring payments with Subscribie - Try Now