Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andreich1980's avatar

Is there a way to find out that user was authenticated via session

Let's say a user signs in checking "remember me" option.

Then the user closes the browser and opens it again. And the user is already signed in.

Is there a way to catch this moment when user just opened my website and he's already signed in?

I can only think of using Illuminate\Auth\Events\Authenticated event, and checking if some variable exists in a session. Then save the variable in the session.

It it doesn't exist - user opens the website for the first time.

Is there a better way of catching the moment?

p.s. It doesn't seem to work or I'm doing it wrong.

LogAuthenticated listener for the Illuminate\Auth\Events\Authenticated event

public function handle(Authenticated $event)
    {
        if (session()->exists('the_first_time_happened')) {
            \Log::debug('Not the first time');
        }
        else {
            \Log::debug('First time!');
            session(['the_first_time_happened' => 1]);
        }
    }
SESSION_DRIVER=file

It shows First time message only once. After that it only shows Not the first time message even if I close the browser and open it again.

Any ideas?

UPD. I used cookies and it seem to work.

if (isset($_COOKIE['first_time'])) {
    \Log::debug('Not the first time');
} else {
    \Log::debug('First time!');
    setcookie('first_time', 1);
}

It shows 'First time' 2 times then 'Not the first time' until I close the browser...

I can't manage it in Laravel way of using cookie. So I'll appreciate any advice!

UPD I guess, I could use a new middleware insteam of those events...

0 likes
0 replies

Please or to participate in this conversation.