2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2016-09-21 17:22:40 +00:00
|
|
|
class Token < ApplicationRecord
|
2016-03-11 06:16:04 +00:00
|
|
|
belongs_to :user
|
|
|
|
|
2016-03-13 08:20:18 +00:00
|
|
|
before_create :assign_token
|
|
|
|
|
|
|
|
CHARS = 32
|
2016-03-11 06:16:04 +00:00
|
|
|
|
|
|
|
private
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2016-03-13 08:20:18 +00:00
|
|
|
def assign_token
|
|
|
|
self.token = generate_token
|
|
|
|
end
|
|
|
|
|
2016-03-11 06:16:04 +00:00
|
|
|
def generate_token
|
2016-03-13 08:20:18 +00:00
|
|
|
loop do
|
|
|
|
candidate = SecureRandom.base64(CHARS).gsub(/\W/, '')
|
2016-07-26 00:14:23 +00:00
|
|
|
return candidate[0...CHARS] if candidate.size >= CHARS
|
2016-03-13 08:20:18 +00:00
|
|
|
end
|
2016-03-11 06:16:04 +00:00
|
|
|
end
|
|
|
|
end
|