blob: 3456c7d0cec75f42e116c933670d8b79462764b8 (
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
26
27
28
29
30
31
32
33
34
35
36
37
38
|
class UserSession < ApplicationRecord
include Queryable
belongs_to :user
has_one :location, as: :locatable
before_create :set_unique_key
attr_readonly :key
scope :active, -> { where("accessed_at >= ?", 2.weeks.ago).where(revoked_at: nil).includes(:user) }
def revoke!
self.revoked_at = Time.now
save!
end
def access(request)
self.accessed_at = Time.now
self.ip = request.ip
self.user_agent = request.user_agent
self.location = Location.build_from_ip(request.ip)
save ? key : nil
end
def browser
@browser ||= BrowserSniffer.new(user_agent)
end
class << self
def authenticate(key)
return nil if key.blank?
self.active.find_by(key: key)
end
end
private
def set_unique_key
self.key = SecureRandom.urlsafe_base64(32)
end
end
|