railslearner
railslearner

Reputation: 1821

Ruby on Rails: perishable_token without Authlogic?

I used signup and login as implemented in http://ruby.railstutorial.org/ without any gems for authentication. However, now that I am trying to activate users by sending them randomly generated URLs, I am having trouble here. I've searched the web and have seen some people that used Authlogic's perishable_token to generate this, but since I haven't used Authlogic at all, I was wondering if there might be a way to generate this without using any other gems. Any advice from rails professionals out there? Thanks

Upvotes: 0

Views: 770

Answers (2)

Jake Dempsey
Jake Dempsey

Reputation: 6312

There are a many ways to achieve this. You first have to decide how you want to generate the token. You can simply create an md5hash using some data on the user such as email along with another piece of seed data like Time.now and store it as the 'perishable_token' on the user.

You can then override the reader accessor for perishable_token to generate another token and update the user model.

before_save :generate_perishable_token

def generate_perishable_token
  self.perishable_token = Digest::MD5.hexdigest("#{Time.now}-#{self.email}")
end

def perishable_token
  generate_perishable_token
  save
end

The above is just some quick pseudo-code, but should get the point across.

Upvotes: 3

nowk
nowk

Reputation: 33171

I suppose a simple way would be to just generate a random string/number to use as the perishable_token

a question about random string generation here you can look at for a starters.

Upvotes: 1

Related Questions