This commit is contained in:
Albert Yi
2018-05-15 14:19:45 -07:00
parent c36efc1f62
commit 8bb890e4d0
4 changed files with 17 additions and 9 deletions

View File

@@ -2,12 +2,14 @@ class ApiKey < ApplicationRecord
belongs_to :user
validates_uniqueness_of :user_id
validates_uniqueness_of :key
has_secure_token :key
def self.generate!(user)
create(:user_id => user.id, :key => SecureRandom.urlsafe_base64(32))
create(:user_id => user.id)
end
def regenerate!
update!(:key => SecureRandom.urlsafe_base64(32))
regenerate_key
save
end
end