I have a scenario where i need to restrict users from having only one active session at a time. Mine is a rails3 application and uses devise for authentication. I’m interested in keeping only the latest user session active. i.e., if a user logs in while there is another session active for the same username, i want to inactivate the older session and allow the recent one. Is there a way, in devise, to get hold of user sessions and invalidate them?
Share
You can track a particular user’s session by storing a unique token specific to that user in the database.
Create a migration to add the field for storing the token. I assume the devise model is User.
Add the following code to
application_controller.rbsign_in(resource_or_scope, *args)is a devise hook that will be called every time the user logs in.invalidate_simultaneous_user_sessionwill log out the current user if another instance of the current user logs in. This will ensure that only one session is active for a user at any instance of time.invalidate_simultaneous_user_sessionfilter should be skipped for the user login action to update the newly logged in user’s token. I am not happy with using a Proc to skip the filter based on controller name and action. If you have already overridden devise’ssessions_controller, then includeskip_before_filter :check_simultaneous_user_sessioninside that controller and you can get rid of the Proc!Hope this helps..