Recently i had to upgrade a rails 2 app to rails 3.2. It wasn’t as difficult as i thought first, but i struggled with one task: authentication. The old app used a very old, not anymore supported version of restful_authentication. Restful_authentication was the then standard plugin for authentication, but is now out-dated. So i decided to switch to devise, a very powerful and often used authentication gem.
To switch from restful_authentication to devise, you have to keep some things in mind. Here is my how-to:
First of all dump your current database. This is really important, because you have to make some changes to your db.
If you didn’t do it yet, install the gem devise. You can found it here https://github.com/plataformatec/devise?source=cr write
gem 'devise'
in your Gemfile. You will also need the gem ‘devise-encryptable’. Install the gems with bundle install. After that install devise related files with the following command in your console in your project folder:
rails generate devise:install
Then tell devise to use your existing user model with this command:
rails generate devise User
After that, you have to change and add some columns to your migration:
## changes from restful_authentication to devise #encrypting passwords and authentication related fields rename_column :users, "crypted_password", "encrypted_password" change_column :users, "encrypted_password", :string, :limit => 128, :default => "", :null => false rename_column :users, "salt", "password_salt" change_column :users, "password_salt", :string, :default => "", :null => false #confirmation related fields rename_column :users, "activation_code", "confirmation_token" rename_column :users, "activated_at", "confirmed_at" change_column :users, "confirmation_token", :string add_column :users, "confirmation_sent_at", :datetime #reset password related fields rename_column :users, "password_reset_code", "reset_password_token" #rememberme related fields add_column :users, "remember_created_at", :datetime #additional field required for devise. #add_index :users, :email, :unique => true add_index :users, :reset_password_token, :unique => true add_index :users, :confirmation_token, :unique => true **optinal** add the following colums if needed add_column :users, "reset_password_sent_at", :datetime ## Trackable add_column :users, "sign_in_count", :integer, :default => 0 add_column :users, "current_sign_in_at", :datetime add_column :users, "last_sign_in_at", :datetime add_column :users, "current_sign_in_ip", :string add_column :users, "last_sign_in_ip", :string
If you do rake db:migrate
, you should have upgraded your database table to be devise conform, but make sure the rake task throughs no errors. If that is the case, overwrite your database with your dump and try again (hopefully you have a dump). I had some issues with indexes so my migration throughs errors and i can’t logged in. After i overwrote the database, removed some indexes in migrations and ran rake db:migrate again, it worked perfectly.
If you were using a very old version of restful_authentication, you have to write your own encryptor. So create a file in config/initializers and call it devise_encryptor.rb and then copy this code in:
# /config/initializers/devise_encryptor.rb require "digest/sha1" module Devise module Encryptable module Encryptors class OldRestfulAuthentication < Base def self.digest(password, stretches, salt, pepper) Digest::SHA1.hexdigest("--#{salt}--#{password}--") end end end end end
Modify your devise.rb to you this encryptor:
config.encryptor = :old_restful_authentication
Also set stretches and pepper:
config.stretches = 1 config.pepper = ''
In your user.rb you have to tell your model to use this new encryptor by adding :encryptable to the devise modules. Don’t forget to install the gem “devise-encryptable”.
If you have a newer version of restful_authentication, you can just use :encryptable, :encryptor => :restful_authentication_sha1
in your user.rb. But this didn’t work for me.
Hopefully i could helped you to upgrade from restful_authentication to devise.
lovely expression, a genuinely useful article. Thank you.reetings. I adhere to your web site to want you continued accomplishment.
thanks a lot for feedback. it was my first blog post at all.