Sign in with devise

What is authentication?

  • Registration
  • Login
  • Change Password
  • Forgot Password

Gem'ing up the works

gem 'devise'
$ bundle install

First step is to add Devise to your Gemfile

And the run bundle install, of course...

But we're not done just yet...

Installing Devise

$ rails g devise:install

In your Terminal/Command Prompt run:

So just like that devise is installed, right?

Devise a model

We need to tell Devise, in our database, where to store all this fun authentication stuff: email, user_name, password.

$ rails g devise User

Don't forget get to rake db:migrate!

+

=

What'd that do?

You can now go in your app's code and find a model (user.rb), that will look like this:

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

Welcome, user

<h1>Welcome#index</h1>

<p>Find me in app/views/welcome/index.html.erb</p>



<% if user_signed_in? %>

  <%= link_to "Sign out",destroy_user_session_path, method: :delete, class: "button" %>

<% else %>

  <%= link_to "Sign in",new_user_session_path %>

<% end %>

Set up your root page so that it allows you to sign in, or knows that you're already signed in.

who's signed in anyway?

You now have access to the Class 'current_user'.

<h1>Welcome#index</h1>

<p>Find me in app/views/welcome/index.html.erb</p>



<% if user_signed_in? %>

  Hello, <%= current_user.email %>!<br />

  <%= link_to "Sign out",destroy_user_session_path, method: :delete, class: "button" %>

<% else %>

  <%= link_to "Sign in",new_user_session_path %>

<% end %>

Devise views

We can customize the sign-in/sign-up forms.

 

First we need to create the views for Devise.

$ rails g devise:views

 A new folder has been created, under 'views', called 'devise', and there's a whole bunch of stuff in there!

 

Does this user have a name?

Devise gives you Email and Password, but wouldn't it be great to have Name, too?

 

We can add attributes to the User just as we would to any Resource.

$ rails g migration AddNameToUsers name:string

And there's one more thing we can't forget to run...

application controller finally becomes useful!

Since we have no User controller, we'll need the help of the Application controller...

  #add the following lines:

  before_action :configure_permitted_parameters, if: :devise_controller?



  protected



  def configure_permitted_parameters

    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit({ roles: [] }, :email, :password,
    :password_confirmation, :name) }

    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, 
    :password_confirmation, :current_password, :name) }

  end

Change your view

We need to also add fields to our devise/registration views (edit & new), so a name can be entered.


  <div><%= f.label :name %><br />
    <%= f.text_field :name, autofocus: true %>
  </div>

autofocus: true means that field will be automatically selected when the page is loaded. I moved this attribute from the e-mail field.

Made with Slides.com