Menu

Setting 2 step confirmation using devise in rails 3

March 31, 2014 - Rails

Setting 2 step confirmation using devise in rails 3
Pre-requirement is

Devise gem, rails framework

first make sure that devise is installed and working successfully with your rails application.

then create a controller called [rails_root]/app/controller/confirmations_controller.rb

then copy and paste mentioned code inside this controller.

class ConfirmationsController ‘confirmations#update’, :via => :put, :as => :update_user_confirmation
end
devise_for :users, :controllers => { :confirmations => “confirmations” }
In user.rb model, add this line.

# new function to set the password without knowing the current password used in our confirmation controller.
def attempt_set_password(params)
p = {}
p[:password] = params[:password]
p[:password_confirmation] = params[:password_confirmation]
update_attributes(p)
end
# new function to return whether a password has been set
def has_no_password?
self.encrypted_password.blank?
end
# new function to provide access to protected method unless_confirmed
def only_if_unconfirmed
pending_any_confirmation {yield}
end
as well as add this extra attribute accesser

devise :database_authenticatable, :registerable,

:recoverable, :rememberable, :trackable, :validatable, :confirmable

Finaly configure environment file for email notification sender

config.action_mailer.default_url_options = { :host => ‘localhost:3000’ }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => ‘smtp.gmail.com’,
:port => 587,
:user_name => ‘myemail@gmail.com’,
:password => ‘mygmailpassword’,
:authentication => ‘plain’,
:enable_starttls_auto => true
}
config.action_mailer.raise_delivery_errors = true

If you use, own email server configuration, just change these values.

in user.rb
uncomment
# Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
add_index :users, :confirmation_token, :unique => true

Now restart the server..you got it..

Leave a Reply