Unlocking the ‘Devise’ Gem

If we have to don our wizarding hat and take out our crystal balls, we would still be harping that Ruby on Rails is going to be hot in 2016 too! 2016 is also going to bring in only 24 hours a day (how we wish, we could stretch the time) and time would still spell money for businesspeople. When time is money, anything which can save time is gold! And that’s where Ruby on Rails come in picture. What could take other programming languages months to complete, takes only weeks, sometimes just days for Ruby on Rails. Shaping ideas into applications was never so easy. With Ruby on Rails, agility is the norm and convention over configuration the benchmark. And what make Ruby on Rails more powerful are the bunch of pre-packaged, categorized, reusable codes, named as gems. With gems, Rails is a super power to behold. One of the most popular gems is called as Devise Gems, which is a complete authentication package. Read on to know more about Devise gem.

What is ‘Devise’ Gem?

  1. Devise is a powerful & flexible authentication solution for Rails.
  2. It is completely based on MVC structure.
  3. It allows to have multiple models signed in at the same time.

Different Modules of Devise

Devise is composed of 10 important modules.

  1. Database Authenticatable: It encrypts and stocks passwords in the database so that the validation could be done for the authenticity of a user at the time of signing in. The authentication can be done both through POST requests or HTTP Basic Authentication.
  2. Omniauthable: Add OmniAuth support i.e. possible to set up a login with OmniAuth.
  3. Confirmable: Sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.
  4. Recoverable: Resets the user password and sends reset instructions.
  5. Registerable: Handles registration process by signing up users also allowing them to edit and destroy their account.
  6. Rememberable: As the name suggests, it helps in managing, generating and clearing a token for remembering the user through saved cookies.
  7. Trackable: Tracks sign in count, timestamps and IP address.
  8. Timeoutable: Expires sessions that have not been active in a specified period of time.
  9. Validatable: Provides validations of email and password. It’s optional and can be customized, so you’re able to define your own validations.
  10. Lockable: It helps in locking an account after a specific number of failed sign-in attempts. The account could be unlocked via email or after specific time period.

Install Devise, Create Model, View & Configure

  1. Create your rails app
  2. Add gem ‘devise’ to your Gemfile.
  3. Run bundle install in your command line.
  4. Execute rails generate devise:install in your command line.
  5. Execute rails generate devise <Model Name> in your command line to create model. Ex:- rails generate devise user. After running rails generate devise user, you should see a user.rb model in your models directory.class User <

    ActiveRecord::Base# Include default devise modules. Others available are:

    # :confirmable, :lockable, :timeoutable and :omniauthable

    devise :database_authenticatable, :registerable,

    :recoverable, :rememberable, :trackable, :validatable

    end

  6. If you want, you can modify your migration file for user. Then run rake db:migrate
  7. Execute rails generate devise:views in your command line to create views
  8. you need to set up the default URL options for the Devise mailer in each environment. Ex- configuration for config/environments/development.rb: config.action_mailer.default_url_options = { host: ‘localhost’, port: 3000 }

You can customize models, controllers and views as per requirement. Please follow the github documents for more details.

Some Controller Filters and Helpers

  • To set up a controller with user authentication before_action :authenticate_user!
  • To verify if a user is signed in or not => user_signed_in?
  • For the current signed-in user => current_user
  • access the session for this scope: => user_session
  • You can also override after_sign_in_path_for and after_sign_out_path_for to customize your redirect paths.

You can integrate ‘activeadmin’ gem, ‘devise_invitable’ gem with devises.

When it comes to time saving, nothing beats Ruby on Rails. If you have an idea and would like to get it implemented, the best bet for you would be to go for Ruby on Rails. RailsCarma has been implementing Ruby on Rails from its nascent stages for development, training, deploying and contributing back to the Rails Community and provide best Ruby on Rails development services. RailsCarma provide end to end Ruby on Rails services including, consulting, architecture, building, management and extension to companies around the globe. You can also hire Ruby on Rails developers with an easy to hire process. Contact Us to know more.

Read About More Gems :

Subscribe For Latest Updates

Related Posts

About Post Author

Leave a Comment

Your email address will not be published. Required fields are marked *