Try this-
if(Auth::check()){
Auth::logout();
}
For checking whether this user is logged in or not, normally we use-
Auth::check()
It returns true or false.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am using laravel 5.4.36 and during the development I noticed that Laravel can't persist users in the session. I've also tried by switching to database session but the same problem occurs.
Any idea why ? Where should I check ?
Try this-
if(Auth::check()){
Auth::logout();
}
For checking whether this user is logged in or not, normally we use-
Auth::check()
It returns true or false.
mmmh I'm not too sure what you mean by that!
What I'm saying is that whenever I use Auth::check() the user already gets logged out..
I have a login form, then some blade templates where I use auth()->check() to test if the user may see some part of the view like some menu elements and so on.
When I log in, everything works fine but the check on the user doesn't seem to be working correctly
I see...
You may be use Session::flush(); to clear all of your existing session first and try again.
Nope, that doesn't work either.
I've tried different browsers, cleared the cache (both browser and with php artisan), tried to check the user in a slightly different way ( by using ! empty(auth()->user()) ) but nothing seem to be working :(
This is my Auth/LoginController
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* LoginController constructor.
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
This is one of the pages where I directly check if the user is logged in, and instead of letting me through (after the login) it redirects me to the login page again.
/**
* User's favourite page
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function usersFavourite()
{
if (false == Auth::check()) {
return redirect('/login');
}
$favouriteRecipes = auth()->user()->favourites('recipes', null, true);
$favouriteIngredients = auth()->user()->favourites('ingredients', null, true);
return view('desk.favourites', compact('favouriteRecipes', 'favouriteIngredients'));
}
It was working up until the other day and I can't spot what was the change that made it not work
Can you show your web.php code?
here it is
Auth::routes();
Route::get('/', 'DeskController@index');
Route::get('/ingredienti', 'DeskController@ingredients');
Route::get('/ricette', 'DeskController@recipes');
Route::get('/utente/{user}', 'DeskController@userProfile');
Route::get('/ingrediente/{ingrediente}', 'DeskController@ingredientDetail');
Route::get('/blogger/{blogger}', 'DeskController@bloggerProfile');
Route::get('/ricetta/{ricetta}', 'DeskController@recipeDetail');
Route::get('/preferiti', 'DeskController@usersFavourite');
Route::get('/home', 'HomeController@index')->name('home');
// Facebook login
Route::get('auth/facebook', 'Auth\RegisterController@redirectToProvider');
Route::get('auth/facebook/callback', 'Auth\RegisterController@handleProviderCallback');
Check that session cookies are being set correctly.
First place to check the path, domain and secure keys in your /config/session.php. Make sure these match your current environment.
I believe I found what the error was!
basically in the menu I had a link for logout which was
<a href="{{ auth()->logout()">logout</a>
I think this was the main problem of my authentication issues .... I feel so ...
Please or to participate in this conversation.