N
N
Nikolai Markov2015-06-01 14:05:47
Ruby on Rails
Nikolai Markov, 2015-06-01 14:05:47

How to make only one active session in devise?

I want to make sure that users can only have one active session. Those. if they come from another browser, then the previous session is closed and a new one is opened.
I thought about doing this by storing the session in the database (activerecord-session_store gem), but it turned out that the usual deletion of a record from the database does not kill the session and when the page is updated, a new record for the session is simply created in the database.
UPD: I forgot to restart the server :-( everything works. Deleting the session from the database works (i.e. the user logs out)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Kononenko, 2015-06-01
@manameiz

If I understand correctly, then you need to make a restriction on one session for one user.
https://github.com/phatworx/devise_security_extension
In the Gemfile we write:
Run the migration generator:
Check the resulting migration. It should be like this:

class AddSessionLimitableToUsers < ActiveRecord::Migration
  def change
    add_column :users, :unique_session_id, :string, limit: 20
  end
end

We apply it:
And in the user model, we add to the call to the devise method: session_limitable
class User < ActiveRecord::Base
  devise ..., :session_limitable
  ...
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question