Róbert Nagy
Róbert Nagy

Reputation: 7602

Laravel authentication check

My navbar should display the Sign up and Login buttons only if the user is not authenticated, otherwise a Log out button.

                @if(Auth::check())
                    <li><a class="navbarButton"  href="auth/register"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
                    <li><a class="navbarButton" href="auth/login"><span class="glyphicon glyphicon-log-in"></span> Login</a> </li>
                @else
                    <li><a class="navbarButton"  href="auth/logout"><span class="glyphicon glyphicon-log-out"></span> Log out</a></li>
                @endif

I know that the user is authenticated, because I get redirected from the login and register pages, but they are still showing in the navbar.

Upvotes: 0

Views: 74

Answers (2)

Reactive
Reactive

Reputation: 97

It seems that you have got your logic the wrong way around here:

@if(Auth::check())
  //this will show if the statement is true (user logged in)
  <li><a class="navbarButton"  href="auth/register"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
  <li><a class="navbarButton" href="auth/login"><span class="glyphicon glyphicon-log-in"></span> Login</a> </li>
@else
  //this will show if the statement is false (no user logged in)
  <li><a class="navbarButton"  href="auth/logout"><span class="glyphicon glyphicon-log-out"></span> Log out</a></li>
@endif

If you reverse it to:

@if(Auth::check())
  //this will show if the statement is true (user logged in)      
  <li><a class="navbarButton"  href="auth/logout"><span class="glyphicon glyphicon-log-out"></span> Log out</a></li>
@else
  //this will show if the statement is false (no user logged in)
  <li><a class="navbarButton"  href="auth/register"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
  <li><a class="navbarButton" href="auth/login"><span class="glyphicon glyphicon-log-in"></span> Login</a> </li>
@endif

Upvotes: 3

Aniruddha Chakraborty
Aniruddha Chakraborty

Reputation: 1867

How about this one by changing Auth::check() with Auth::user()

            @if(Auth::user())
                <li><a class="navbarButton"  href="auth/register"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
                <li><a class="navbarButton" href="auth/login"><span class="glyphicon glyphicon-log-in"></span> Login</a> </li>
            @else
                <li><a class="navbarButton"  href="auth/logout"><span class="glyphicon glyphicon-log-out"></span> Log out</a></li>
            @endif

Upvotes: 0

Related Questions