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?
相关问题
- Strong parameter override for DeviseTokenAuth cont
- Eager-loading association count with Arel (Rails 3
- Rails simple model attributes not saved to databas
- jquery-ui progressbar not showing
- PDF attachment in email is called 'Noname'
相关文章
- “No explicit conversion of Symbol into String” for
- Rspec controller error expecting <“index”> but
- Factory_girl has_one relation with validates_prese
- Rails: Twitter Bootstrap Buttons when visited get
- is there a “rails” way to redirect if mobile brows
- Got ActiveRecord::AssociationTypeMismatch on model
- superclass mismatch for class CommentsController (
- rails 3, how add a simple confirmation dialog when
For updated devise for rails 4, you may change the code according to this
http://pastebin.com/p6mvC8T3
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.rb
sign_in(resource_or_scope, *args)
is a devise hook that will be called every time the user logs in.invalidate_simultaneous_user_session
will 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_session
filter 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_session
inside that controller and you can get rid of the Proc!Hope this helps..