0

I am building a simple log in app and I want to allow users to login from one browser at a time.

In other words, if users are logged in in Safari, and then open a Firefox browser and login into the app, Safari app should log them out. Likewise, if users then try to login again into Safari, Firefox app should kick them out. Can some one help me in this. Currently I am using Laravel's out of the box login module to make development fast by using command PHP ARTISAN MAKE:AUTH.

Kindly guide me.

Unbreakable
  • 7,776
  • 24
  • 90
  • 171

2 Answers2

2

How I would do it: I would consider adding a 'user_agent' column to the 'users' table in your database, in which you can record the browser type each time a user logs in (using $_SERVER['HTTP_USER_AGENT'] or the like...). Once that's in place, you can add a Middleware that will compare the stored user agent against the current user agent and initiate a logout if the two don't match.

However, this will only log the user out of the Safari browser once they have logged into the Firefox browser and then they try to go back to Safari. If you need the Safari browser to logout immediately as soon as the user logs into a different browser, you can simply add an ajax polling function that will trigger the Middleware even when the user is not actively using the site (and this way the Safari browser will logout as soon as the user logs into Firefox etc).

Again, I'm sure there are many ways to butter this slice of toast, these are just my initial thoughts...

Community
  • 1
  • 1
IzzEps
  • 582
  • 6
  • 20
  • Suppose, user logs into FF and then without logging out just closes the browser. What will happen? Does he have any chance to log in from another browser at a later time ? – Istiaque Ahmed Aug 03 '18 at 17:09
1

It's pretty simple – when you log users in (most probably postLogin() method of your AuthController), in case of success - store the current session ID in the database, in User model. Then, in your 'auth' middleware, add an extra check – compare user's session ID from the model with PHP session ID. User will be kicked out in other browsers or sessions upon next click.

Steps in more detail:

  1. Add a migration that adds a current_session field to User table.
  2. Persist current session ID in postLogin()
  3. Compare session ID from the database with PHP session ID. Optionally, destroy the session in case of mismatch
Denis Mysenko
  • 6,366
  • 1
  • 24
  • 33
  • Thank you for your response. Since I am beginner I could not understand properly the solution. Anyway shall I first change setting in session.php file from driver--> file to driver-->database. so that session data can be put in database. I am beginner so kindly bear with me. – Unbreakable May 18 '16 at 19:26