Module: Devise::Models::RevocableSession

Extended by:
ActiveSupport::Concern
Defined in:
lib/devise_revocable_session/models/revocable_session.rb

Instance Method Summary collapse

Instance Method Details

#activate_session(request) ⇒ Object

session



18
19
20
21
22
23
24
25
26
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 18

def activate_session(request)
  new_session = revocable_sessions.new((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



36
37
38
39
40
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 36

def deactivate_session!(session_id)
  devise_writer_wrapper do
    revocable_sessions.where(session_id: session_id).delete_all
  end
end

#devise_writer_wrapperObject



58
59
60
61
62
63
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 58

def devise_writer_wrapper
  role = Rails.application.config.active_record.writing_role || :writing
  ActiveRecord::Base.connected_to(role: role) do
    ActiveRecord.legacy_connection_handling
  end
end

#exclusive_session(session_id) ⇒ Object



28
29
30
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 28

def exclusive_session(session_id)
  revocable_sessions.where('session_id != ?', session_id).delete_all
end

#has_revocable_sessions?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 13

def has_revocable_sessions?
  true
end

#mark_last_seen!(device_id) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 48

def mark_last_seen!(device_id)
  devise_writer_wrapper do
     = revocable_sessions.find_by(device_id: device_id)
    #skip second to reduce database hit
    if (Time.now - (.last_seen_at)) >= 60
      .update_column :last_seen_at, Time.now
    end
  end
end

#purge_old_sessionsObject



42
43
44
45
46
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 42

def purge_old_sessions
  devise_writer_wrapper do
    revocable_sessions.order(last_seen_at: :desc).offset(10).destroy_all
  end
end

#session_active?(device_id, session_id) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/devise_revocable_session/models/revocable_session.rb', line 32

def session_active?(device_id, session_id)
  revocable_sessions.where(device_id: device_id, session_id: session_id).exists?
end