You can nudge your life in a new direction every day.

Determine Login State and Information about Logged In User with Laravel 8 & Fortify

Submitted by admin on Thu, 05/20/2021 - 15:05

This wasn't completely apparent to me so I thought I would jot it down.

If you just need to check if logged in state for the user you can use check method in the Auth class

<?php

if (!Auth::check())
{
    print 'no';
  
    
}
if (Auth::check())
{
    print 'yes';
  
}

?>

If you want access to the properties of the user object you need to make sure you use the auth middle on your route

 

Route::get('/home', function () {
    return view('welcome');
})->middleware('auth');

Now you will have access to 

The logged in users id, email or whatever other attribute you allow.

 

<?php

if (!Auth::check())
{
    print 'no';
    //print_r(Auth);
    
}
if (Auth::check())
{
    print 'yes';
    print_r(Auth::user());
    print Auth::user()->email;
}

?>