blob: 9dae085773aa224901913192ed062e54910323bc (
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
39
40
41
42
43
44
45
46
47
|
class UserSession < ApplicationRecord
include Queryable
belongs_to :user
has_one :location, as: :locatable
before_create :set_unique_key
attr_readonly :key
scope :active, -> do
where("accessed_at >= ?", 20.minutes.ago)
.where("created_at >= ?", 1.day.ago)
.where(revoked_at: nil)
.includes(:user)
end
def revoke!
self.revoked_at = Time.current
save!
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 ? 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
def sweep(time = 1.day)
delete_all("accessed_at < ?", time.ago)
end
end
private
def set_unique_key
self.key = SecureRandom.urlsafe_base64(32)
end
end
|