metamaps--metamaps/app/models/token.rb
2016-03-13 00:56:10 -08:00

22 lines
357 B
Ruby

class Token < ActiveRecord::Base
belongs_to :user
before_create :assign_token
CHARS = 32
private
def assign_token
self.token = generate_token
end
def generate_token
loop do
candidate = SecureRandom.base64(CHARS).gsub(/\W/, '')
if candidate.size >= CHARS
return candidate[0...CHARS]
end
end
end
end