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?
- Devise is a powerful & flexible authentication solution for Rails.
- It is completely based on MVC structure.
- It allows to have multiple models signed in at the same time.
Different Modules of Devise
Devise is composed of 10 important modules.
- 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.
- Omniauthable: Add OmniAuth support i.e. possible to set up a login with OmniAuth.
- Confirmable: Sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.
- Recoverable: Resets the user password and sends reset instructions.
- Registerable: Handles registration process by signing up users also allowing them to edit and destroy their account.
- Rememberable: As the name suggests, it helps in managing, generating and clearing a token for remembering the user through saved cookies.
- Trackable: Tracks sign in count, timestamps and IP address.
- Timeoutable: Expires sessions that have not been active in a specified period of time.
- Validatable: Provides validations of email and password. It’s optional and can be customized, so you’re able to define your own validations.
- 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
- Create your rails app
- Add gem ‘devise’ to your Gemfile.
- Run bundle install in your command line.
- Execute rails generate devise:install in your command line.
- 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
- If you want, you can modify your migration file for user. Then run rake db:migrate
- Execute rails generate devise:views in your command line to create views
- 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 :