Single session / login

Closed
Vanguard - Advanced PHP Login and User Management Vanguard - Advanced PHP Login and User Management August 26, 2020
Login to reply
Dennis Zervas
3 years ago

Thank you Milos.

I tried this and it works !


regards


Dennis

Milos Stojanovic Support Agent
3 years ago

Hey Dennis,

Unfortunately, out of the box, you cannot limit the user to one active session. However, since Vanguard supports the database session driver, it's easy enough to customize it to allow only one active session per user. 

A high-level overview of what you need to do is the following:

1. Make sure that you use a database session driver.

2. Update the login method within app/Http/Controllers/Web/Auth/LoginController.php controller and add the following check right after "$user->isBanned()" if statement:

// ...

$activeSessions = app(\Vanguard\Repositories\Session\SessionRepository::class)
    ->getUserSessions($user->id);

if (count($activeSessions) >= 1) {
        return redirect()->to('login' . $to)
            ->withErrors(__('You are already logged in on another device.'));
} // ...

Kind regards,
Milos

Dennis Zervas
3 years ago

Hi Milos,


sorry for the many questions, I'm just trying to get this working for a project.

I need to set Vanguard so that a user can only login once and cannot share his/her login with someone else.

So if someone else logs in, it should not allow it or logout the first user.

I could not find anything such in the documentation.

Is there a setting somewhere ?

What is your recommendation please?


thank you


Dennis Zervas