metamaps--metamaps/app/models/token.rb

22 lines
368 B
Ruby
Raw Permalink Normal View History

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