I recently had my first real experience with Devise and I wanted to review what I did and how here!
What is Devise?
First, let's talk a little bit about Devise what it is and what it can do for you!
Devise is a flexible authentication solution for Rails based on Warden. It:
Is Rack based;
Is a complete MVC solution based on Rails engines;
Allows you to have multiple models signed in at the same time;
Is based on a modularity concept: use only what you really need.
Devise is comprised of 10 modules, you can view them Here.
Step one - Installing Devise
To install Devise, similar to other Ruby gems you can add gem 'devise'
to your gemfile, then run bundle install
Step two - Run the Generator
Next, you'll need to run the generator like so: rails generate devise:install
Once you have run the generator you will see a set of instructions in your console. In those instructions you will see that you need to setup the default URL for Devise mailer. Here's an example of what I used and you can also find this example within the instructions or Here
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
The generator will install an initializer with all of that describes all of Devises' configuration options. It is highly recommended that you review this. You can find it within your app at app/config/devise.rb
Step three - Add Devise to your models
Now you can add Devise to the models of your choice, to do this run the following in your terminal and replace MODEL with the name of your model. rails generate devise MODEL
There are additional configurations options you can add to your models, see the app/config/devise.rb
file to learn more. Once you have configured the options you can then run rails db:migrate
to create the migration. Devise comes with some helpers such as authenticate
if you have a User model and you want to authenticate the user prior to processing any actions you can add this line of code at the top of your controller before_action :authenticate_user!
(This is assuming your model is User
) Here are a few other helpers associated with Devise.
To verify if a user is signed in, use the following helper:
user_signed_in?
For the current signed-in user, this helper is available:
current_user
You can access the session for this scope:
user_session
Step four - Assigning a root path
Devise expects your to have a root path assigned in our app. Navigate to app/config/routes.rb
and assign a root path like so, root to: 'home#index'
Step five - Configuring Views
To add Devise to your views run rails generate devise:views
I recommend checking out the documentation to see how you can customize your views when/if needed.
Step six - Configuring Controllers
Similar to configuring views, you can also configure your controllers, again it's a good idea to check the documentations to see how to customize your controllers with Devise. rails generate devise:controllers [scope]
Note: You will also need to have your controllers inherit from < Devise::[controllername]
instead of < ApplicationController
Example: If I have a Registrations controller it would look like this
class RegistrationsController < Devise::RegistrationsController
end
Step seven - Configuring Routes
Devise ships with default routes, if you need to customize them you can do that by using the devise_for
method. See the example below
devise_for :users, path: 'auth', path_names: { sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification', unlock: 'unblock', registration: 'register', sign_up: 'cmon_let_me_in' }
There are many customizations you can make with Devise but this guide should help you get started if you're unfamiliar with it. I definitely recommend reviewing the gem documentation to learn more and see what all the customizations are!
Top comments (0)