Pages

Monday, 16 November 2015

restful_authentication to Devise Migration


  • Add the following gem into your Gemfile
    • gem 'devise'
    • gem 'devise-encryptable'
  • Run the following commands from console
    • bundle intstall
    • rails g devise:install
  • In model file whatever you are using saving user information like user.rb
    • devise :database_authenticatable, :registerable, :recoverable, :rememberable, :confirmable, :validatable, :encryptable, :encryptor => :restful_authentication_sha1
          Modules :encryptable and :encryptor => :restful_authentication_sha1 are important here.
  • In config/initializers/devise.rb, configure as below
config.stretches = 1 config.pepper = ""

We are setting empty pepper and stretches to 1. This is how restful authentication works. It does not use stretches or pepper (by default).

( Note: You may find REST_AUTH_SITE_KEY in config/initializers/site_keys.rb )

In other case if You did set the REST_AUTH_SITE_KEY , then configure as below
config.stretches = 10
  config.pepper = "<YOUR_REST_AUTH_SITE_KEY>"

  • If you're using :validatable you may have to adjust your minimum password length. Devise uses 8, restful_authentication uses 6.
  • In config/initializers/devise.rb
                    config.password_length = 6..128

That is it for configuration. Now we have to change the table column names (without deleting them) to give devise what it wants. Here is the migration file I used. Change it according to your app's business logic.
  • Add migration to change columns
class ConvertRaToDevise < ActiveRecord::Migration
def self.up
  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
  add_column :users, "remember_created_at", :datetime
end

def self.down
  remove_column :users, "remember_created_at"
  rename_column :users, "encrypted_password", "crypted_password"
  change_column :users, "crypted_password", :string, :limit => 40
  rename_column :users, "password_salt", "salt"
  change_column :users, "salt", :string, :limit => 40
end
end

You will able to use devise as authentication for your project.