Determining if a Laravel User is Authenticated

Authentication Forms Views

Committed to the LaraBrain by: wjgilmore (@wjgilmore) at May 10, 2016 4:25 pm

Laravel application users often must be authenticated before access is granted to certain site features

Laravel provides a number of useful features for validating and managing a user's authentication state. To determine whether a user is currently authenticated, use the Auth class' static check method. I'll often use check to conditionally present links to restricted parts of the application, such as the user's administration interface:

@if (\Auth::check()) 
  {!! HTML::link('admin', 'Admin') !!}

Of course, you'll want to further restrict any conditionally-presented routes using middleware to ensure unauthenticated users don't enter restricted areas (either mistakenly or otherwise).

I'll also use the \Auth::check() in conjunction with form requests, adding it to the authorize() method in order to ensure the user is authenticated before any further form processing is done:

public function authorize()
    return \Auth::check();