Comment on page
Authentication with Devise
gem 'devise'to your Gemfile and
rails g devise:install
Devise will give you some setup instructions. We don't need to worry about most of them, but we do need to set a root URL. Usually, you will point the root URL to the index action of some important resource in your application: In
This is just a shortcut for setting a root URL the old way,get "/", :controller => "photos", :action => "index"
Next, we need to secure one of our models with Devise. If you already have a model (like User) created, skip down to the "Add Devise for existing model" section. Otherwise continue with the "Generate a new model with Devise" section.
Use the following command to generate a User model with Devise built-in. Replace the column names with ones that are relevant to your application.
rails g draft:devise user username:string avatar_url:string
rake db:migrateand restart your server.
Skip down to "benefits" if you've already generated a new model with Devise. You don't need to do both.
If you already have rows in the users table and don't want to drop your database, you will have to go through a couple of extra steps to prevent errors.
- First, add a column called "email" if you don't already have one.
- Second, make sure that every existing row has a unique value for email.Then,rails g draft:devise user
- Finally, go into the migration file called "add_devise_to_users" and comment out the line that adds an email column.andrails db:migrateand restart your server.
The big benefits that we now get for free from Devise are:
- RCAVs that handle sign up, sign in, and sign out -- all done, for free!
current_userhelper method, available within all views and controllers, that will retrieve the row from the Users table for whoever is currently signed in
before_action :authenticate_user!filter that we can use in our controllers to ensure someone is signed in before accessing any actions within that controller
There are lots more (password reset emails, etc), but these are the first ones that we care about.
The application layout file that is written by
rails g starter:style defaultincludes examples in the nav bar of what the sign-up/in/out links look like. You'll have to customize them a bit based on what you called your secure model. Notice the
data-method="delete"attribute on the sign-out link -- that is important, and you need to include it. (But be careful not to include it on regular links if you, e.g., copy-paste this one.)
Devise does an incredible amount of work for us out of the box, but at some point, we will want to customize it; at the very least, we will want to make the sign-up and sign-in forms look nicer.
Assuming we are using
before_action :authenticate_user!in our
ApplicationControllerto force visitors to sign up or sign in before doing anything else, then the sign in page is our landing page, after all (try going to Twitter, Facebook, etc when signed out -- the landing page is really a sign-in page with some extra info thrown in). So it would be nice to be able to make it pretty.
More importantly, we will likely want to allow users to provide more information when they sign up or edit their profiles than simply their email addresses and passwords.
The first problem is that we don't even have any code to edit in order to customize the view templates for sign-in, sign-up, edit profile, etc. That's because, by default, these templates are wrapped up inside the Devise gem.
It's really easy to have Devise generate copies of these templates that we can edit, though. And then our edited versions will take precedence. Simply run
rails g devise:views
There will now be a folder in your
devise, with a whole bunch of stuff in it. What we are most interested in are the contents of the
app/views/devise/registrations/new.html.erbis the sign up form,
registrations/edit.html.erbis the edit profile form, and
sessions/new.html.erbis the sign in form.
Devise's markup is a bit more advanced than what we had time to get to in class. We have, until now, only been writing raw HTML code by hand.
In practice, Rails developers often use built-in Ruby helper methods to generate HTML. This provides benefits in terms of security, brevity, etc.
Devise is using some of these helpers to draw the
<input>elements in its forms, so we don't directly see those things in the view templates.
Instead, we see something like
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
etc. Basically, the
form_forhelper method is spitting out the
<form>tag, and each of the
f.____fieldmethods are spitting out the
You can write whatever HTML you want around these helpers; for example, Devise has already wrapped each label/input pair inside a
You can also add CSS classes to the input tags they generate, like so:
<%= f.password_field :password, :class => "form-control" %>
There are various types of
____fieldhelpers; the most common are
f.collection_select-- this one is special. It is similar to the
options_from_collection_for_selecthelpers we discussed in class, but rolled into one. The syntax looks like this (assuming you have a Company table and you want the user to belong to one company):<%= f.collection_select :company_id, Company.all, :id, :name %>
You can add as many of these as you need for the additional columns you've included in your user model.
The last step we need to take is to whitelist these additional attributes as things that we will allow users to modify about themselves. To do this, you need to go to your
ApplicationControllerand add the following code:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
devise_parameter_sanitizer.permit(:sign_up, :keys => [:username, :avatar_url])
devise_parameter_sanitizer.permit(:account_update, :keys => [:avatar_url])
You need to add the name of each column you want to be able to modify to the respective array of symbols. In the example, I have whitelisted both username and avatar_url to be modified upon sign-up, but only avatar_url can be modified upon account update. You have to decide what makes sense in your app.