1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/services/generate_token_service.rb
2022-08-19 20:03:09 +02:00

22 lines
393 B
Ruby

# 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