1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/services/generate_token_service.rb

22 lines
393 B
Ruby
Raw Normal View History

2022-08-19 19:59:13 +02:00
# frozen_string_literal: true
# Generate a unique token
class GenerateTokenService
def call(model_class = Order)
loop do
token = "#{random_token}#{unique_ending}"
break token unless model_class.exists?(token: token)
end
end
private
def random_token
SecureRandom.urlsafe_base64(nil, false)
end
def unique_ending
(Time.now.to_f * 1000).to_i
end
end