mirror of
https://github.com/ryankazokas/turbovault-app.git
synced 2026-04-16 23:22:53 +00:00
Moving to github
This commit is contained in:
27
app/models/api_token.rb
Normal file
27
app/models/api_token.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
class ApiToken < ApplicationRecord
|
||||
belongs_to :user
|
||||
|
||||
# Validations
|
||||
validates :token, presence: true, uniqueness: true
|
||||
|
||||
# Callbacks
|
||||
before_validation :generate_token, on: :create
|
||||
|
||||
# Scopes
|
||||
scope :active, -> { where("expires_at IS NULL OR expires_at > ?", Time.current) }
|
||||
|
||||
# Instance methods
|
||||
def expired?
|
||||
expires_at.present? && expires_at < Time.current
|
||||
end
|
||||
|
||||
def touch_last_used!
|
||||
update_column(:last_used_at, Time.current)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_token
|
||||
self.token ||= SecureRandom.urlsafe_base64(32)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user