user4584963
user4584963

Reputation: 2523

How to obfuscate user id with random number using friendly_id

When my new website launches there will be very few users. Currently a user's profile page is /users/:id so in the early stages it will be /users/6, etc. I don't want others to know how many users the website has. I think that an long id (such as a uuid) with numbers and letters looks ugly so I would prefer it just be numbers.

How can I use friendly_id gem to create a random number slug that will also be unique? This is also my first website so any "best practices" tips regarding user id obfuscation would be helpful. Thanks.

Upvotes: 1

Views: 671

Answers (1)

Bart
Bart

Reputation: 2656

This is an example from existing project:

class User < ActiveRecord::Base
  extend FriendlyId
  friendly_id :email, use: [:slugged, :finders]

  def normalize_friendly_id(email)
    Digest::MD5.hexdigest(email)
  end

  def should_generate_new_friendly_id?
    slug.blank?
  end
end

You can change the MD5 to a random number using something like this:

SecureRandom.random_number(1_000_000_000)

Upvotes: 1

Related Questions