How to verify an authenticated user in Blade

The typical method used to verify an authenticated user in Blade is:

@if (auth()->check())
// logic if user is authenticated
@else
// logic otherwise
@endif

The code above shows how to verify authenticated users using the auth()->check() command:

  • The auth() method returns the authenticated users in your application.
  • The check() method is used to verify that the current user has been authenticated.

Although the above method is suitable for most purposes, a shorter way that Laravel offers is to use the @auth and @endauth directives. This is shown below:

@auth
// logic goes here
@endauth

The @auth directive determines if a user has been authenticated.

You can place any code snippet you want inside the @auth and @endauth directives to run or display for a particular authenticated user. Since the @endauth is like a closing tag for the @auth directives, you can wrap the logic between the @auth and endauth directives without needing to use the @if directive.

Free Resources