diff --git a/app/models/access_token.rb b/app/models/access_token.rb index 5f52ad8..418c6ff 100644 --- a/app/models/access_token.rb +++ b/app/models/access_token.rb @@ -11,5 +11,35 @@ # class AccessToken < ActiveRecord::Base + TERM_OF_VALIDITY = 2.weeks + belongs_to :user + + validates(:user_id) { presence } + validates(:token) { presence; uniqueness } + + before_create :generate_token, :update_last_activity_time + + def self.delete_expired + self + .where + .not(last_activity_at: (Time.now - TERM_OF_VALIDITY)..Time.now) + .delete_all + end + + def expired? + Time.now - TERM_OF_VALIDITY > self.last_activity_at + end + + def update_last_activity_time + self.last_activity_at = Time.now + end + + private + + def generate_token + begin + self.token = SecureRandom.hex(32) + end while self.class.find_by(token: self.token) + end end