Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
blade
laravel
communitycreator

How to verify an authenticated user in Blade

Chinweuba Elijah Azubuike

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.

RELATED TAGS

php
blade
laravel
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring