Reputation:
I recently updated an app and push to heroku, one of the features updated was changing my session id from the user id to a secure hex. Once pushed i noticed that pervious users could no longer log in. I checked the heroku console and found out that their authentication token was set to nil which is expected i guess. my questions are:
Is there a way to set authentication token for the users prior to update? Can I write some sort of loop to create this token for users prior to update to prevent this from happening again?
or do i have to just wipe the database and let users sign up again.
cheers.
Upvotes: 1
Views: 591
Reputation: 6931
Solution from Remember Me & Reset Password comments section.
TRY IT ON YOUR DEVELOPMENT DATABASE
I try it some time ago and it work it will update authentication token for all your users if generate_token is the method name and auth_token is a DB field where you store a token. You can thinker with if statement inside task if you just need to change it for users who don't have a token at the moment.
create file in lib/tasks/rebuild_token_auth.rake:
namespace :user do
desc "Rebuild Auth-Tokens"
task :rebuild_auth_token => :environment do
User.transaction do
User.all.each { |u|
u.generate_token(:auth_token)
u.save!
}
end
end
end
call it like this:
rake user:rebuild_auth_token
In his example generate_token looks like this:
class User < ActiveRecord::Base
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
end
Upvotes: 2