blob: dd87344aa67d3b31051553af3430e8a28a50eced (
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 Token < CouchRest::Model::Base
use_database :tokens
belongs_to :user
# timestamps! does not create setters and only sets updated_at
# if the object has changed and been saved. Instead of triggering
# that we rather use our own property we have control over:
property :last_seen_at, Time, accessible: false
validates :user_id, presence: true
def authenticate
if expired?
destroy
return nil
else
touch
return user
end
end
def touch
self.last_seen_at = Time.now
save
end
def expired?
expires_after and
last_seen_at + expires_after.minutes < Time.now
end
def expires_after
APP_CONFIG[:auth] && APP_CONFIG[:auth][:token_expires_after]
end
def initialize(*args)
super
self.id = SecureRandom.urlsafe_base64(32).gsub(/^_*/, '')
self.last_seen_at = Time.now
end
design do
end
end
|