3
How to customize Devise authentication in Rails3? Devise is an authentication solution for Rails application development. Since devise is an engine and all the files created by it are packaged inside the gem. We need to invoke some generators in order to customize it according to our choice. Configuring views If you want to modify the views generated by devise, then you just need to invoke the following generator, and it will copy all views to your application. rails generate devise:views If you have more than one role in your application (such as "User" and "Admin"), Devise offers an easy way to customize views. Just write the following line inside your "config/initializers/devise.rb" config.scoped_views = true Now you will be able to have views based on the role like "users/sessions/new" and "admins/sessions/new". You can also use the generator to generate scoped views like below: rails generate devise:views users Configuring controllers If the customization at the views level is not enough, you can customize the controller generated by devise by following these steps. Step#1 Create a custom controller

How to customize devise authentication in rails3

Embed Size (px)

Citation preview

Page 1: How to customize devise authentication in rails3

How to customize Devise authentication in Rails3?

Devise is an authentication solution for Rails application development. Since devise is an engine and all the files created by it are packaged inside the gem. We need to invoke some generators in order to customize it according to our choice.

Configuring views

If you want to modify the views generated by devise, then you just need to invoke the following generator, and it will copy all views to your application.

rails generate devise:views

If you have more than one role in your application (such as "User" and "Admin"), Devise offers an easy way to customize views. Just write the following line inside your "config/initializers/devise.rb" config.scoped_views = true

Now you will be able to have views based on the role like "users/sessions/new" and "admins/sessions/new". You can also use the generator to generate scoped views like below:

rails generate devise:views users

Configuring controllers

If the customization at the views level is not enough, you can customize the controller generated by devise by following these steps.

Step#1

Create a custom controller

class Users::SessionsController < Devise::SessionsController[Your code goes here]

end

Page 2: How to customize devise authentication in rails3

Now you can customize your methods according to the conditions.

Step#2

Now tell the route to use this controller in “config/routes.rb”

devise_for :users, :controllers => { :sessions => "users/sessions" }

Customizing Error Messages

Devise has its own error messages that are shown when something goes wrong. All of these messages are stored in a locale file (config/locales/devise.en.yml), making it easy to maintain them. Here you can see the list of error messages created by devise and you can customize them according to your choice.

Customizing Registration process

You can also customize the registration process of devise. Here is an example to explain the customization of Registration process where devise sends an activation email automatically after a new user registration happens.

Step#1

Modify the “users” migration file created by devise. Uncomment the block of fields under Confirmable.

t.string :confirmation_token t.datetime :confirmed_at t.datetime :confirmation_sent_at t.string :unconfirmed_email

Step#2

Customize the “User” model like below:

devise :database_authenticatable, :registerable,:recoverable, :rememberable, :trackable, :validatable, :confirmable

We have added “:confirmable” module for devise to the model. Confirmable is responsible to verify if an account is already confirmed to sign in, and to send emails with confirmation instructions. Confirmation instructions are sent to the user email after creating a record.Then add the field names that are defined in step#1 to the attr_accessible.

Step#3

Page 3: How to customize devise authentication in rails3

Run the migration

rake db:migrate

Step#4

Now set the default URL for according to your requirement in “config/enviroments/development.rb”

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

Now if you register for a new user then devise will send a confirmation email with required instructions.