Cannon Moyer
Cannon Moyer

Reputation: 3164

Generate Secure Token - Ruby on Rails

I have a process flow where I send a user a link via email with a generated token.

I'm currently generating the token like this:

before_create :generate_token

protected

def generate_token
  self.token = loop do
    random_token = SecureRandom.urlsafe_base64(nil, false)
    break random_token unless ModelName.exists?(token: random_token)
  end
end

The user can access the record by going to

mysite.com/records/:token

How can I accomplish the same task with either a hashed or encrypted token? I want to make this link more secure.

Upvotes: 0

Views: 201

Answers (1)

user3309314
user3309314

Reputation: 2543

Maybe you are looking for ActiveSupport::MessageVerifier.

MessageVerifier makes it easy to generate and verify messages which are signed to prevent tampering.

How Does MessageVerifier Work?

Upvotes: 2

Related Questions