Module: Devise::Models::RevocableSession
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/devise_revocable_session/models/revocable_session.rb
Instance Method Summary collapse
-
#activate_session(request) ⇒ Object
session.
- #deactivate_session!(session_id) ⇒ Object
- #exclusive_session(session_id) ⇒ Object
- #has_revocable_sessions? ⇒ Boolean
- #mark_last_seen!(device_id) ⇒ Object
- #purge_old_sessions ⇒ Object
- #session_active?(device_id, session_id) ⇒ Boolean
Instance Method Details
#activate_session(request) ⇒ Object
session
16 17 18 19 20 21 22 23 24 |
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 16 def activate_session(request) new_session = revocable_sessions.new(attrs_for_login(request)) new_session.session_id = SecureRandom.hex(64) new_session.device_id = SecureRandom.uuid new_session.signed_in_ip = request.remote_ip new_session.save purge_old_sessions new_session end |
#deactivate_session!(session_id) ⇒ Object
34 35 36 |
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 34 def deactivate_session!(session_id) revocable_sessions.where(session_id: session_id).delete_all end |
#exclusive_session(session_id) ⇒ Object
26 27 28 |
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 26 def exclusive_session(session_id) revocable_sessions.where('session_id != ?', session_id).delete_all end |
#has_revocable_sessions? ⇒ Boolean
11 12 13 |
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 11 def has_revocable_sessions? true end |
#mark_last_seen!(device_id) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 42 def mark_last_seen!(device_id) login_record = revocable_sessions.find_by(device_id: device_id) #skip second to reduce database hit if (Time.now - (login_record.last_seen_at)) >= 60 login_record.update_column :last_seen_at, Time.now end end |
#purge_old_sessions ⇒ Object
38 39 40 |
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 38 def purge_old_sessions revocable_sessions.order(last_seen_at: :desc).offset(10).destroy_all end |
#session_active?(device_id, session_id) ⇒ Boolean
30 31 32 |
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 30 def session_active?(device_id, session_id) revocable_sessions.where(device_id: device_id, session_id: session_id).exists? end |