summaryrefslogtreecommitdiff
path: root/app/models/user_session.rb
blob: b9e56758f4d8ed9b74e7faa8dd6f7695e6136c43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class UserSession < ApplicationRecord
  has_one :location, as: :locatable
  belongs_to :user
  scope :active, -> do
    where("accessed_at > ?", 2.weeks.ago).where(revoked_at: nil)
  end

  def revoke!
    update_attribute(:revoked_at, Time.current)
  end

  def access(request)
    self.accessed_at = Time.current
    self.ip = request.ip
    self.user_agent = request.user_agent
    self.location = Location.build_from_ip(request.ip)
    save ? id : nil
  end

  class << self
    def authenticate(id)
      active.find_by(id: id)
    end
  end
end