Skip to content

User Authentication

domtunstill edited this page Nov 7, 2019 · 4 revisions

Installation of Devise

https://github.com/plataformatec/devise

1. Adding Devise Gem to 'gemfile'

Add gem 'devise' to your gemfile, then bundle install

2. Installing Devise Setup Files

Run $ rails g devise:install // 'g' is short for generate

3. Adding Default URL

Ensure you have set a default url, for example: root to: "home:index" in config/routes.rb

4. Add Default URL For Developer Environemt

Add default url for each environment. for example: config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } within config/environments/development.rb

5. Generate Devise Views

Run $ rails g devise:views to automatically create a devise directly under views

6. Generate 'User' Model For Devise

Create your user model by running rails g devise User

7. Migrate Databases To Add User Table

Run $ rails db:migrate (if tables exist, first run $ rails db:drop followed by rails db:setup)

8. Don't forget your other environments!

Migrate Test Databases

Extras

  • Add flash messages to application.html.erb (instructions included when you run $ rails g devise:install)

Setup For Tests

1. Create a Welcome Page Using Rails

Run $ rails g contoller Welcome

2. Create Feature Test For Sign up

Within spec/features/user_can_sign_up_spec.rb

3. Add 'Sign up' Link To Welcome Page and Passed the Test

Added <%= link_to "Sign Up", new_registration_path(User) %><br /> within views/welcome/index.html.erb

4. Create Feature Test For Log In

Within spec/features/user_can_log_in_spec.rb

5. Create 'web_helpers' File and 'create user' Method

Added web_helpers.rb file to 'spec' folder

Added

def create_user 
    User.create(first_name: 'John', last_name: 'Doe', email: '[email protected]', password: 'password')
end

within spec/web_helpers.rb

6. Requrie 'web_helpers' Within 'spec_helper'

Added require 'web_helpers' within spec/spec_helpers.rb

7. Add 'Log in' Link To Welcome Page and Passed the Test

Added <%= link_to "Log In", new_session_path(User) %><br /> within views/welcome/index.html.erb

8. Create Feature Test For Log Out

Within spec/features/user_can_log_out_spec.rb

9. Add 'Log out' Link To Welcome Page and Passed the Test

Added <%= link_to('Logout', destroy_user_session_path, method: :delete) %> within views/welcome/index.html.erb

Add Extra Fields for First Name and Last name

1. Create a Db Migration

Run $rails generate migration AddNamesToUsers

2. Add Changes To Migration File

Added

    add_column :users, :first_name, :string, null: false
    add_column :users, :last_name, :string, null: false
    add_column :users, :avatar, :string

within db/migrate/20191105146713_add_names_to_user.rb

3. Add User Seeds

Added

    users = User.create([
    {first_name: 'John', last_name: 'Doe', email: '[email protected]', password: 'password'},
    {first_name: 'Anne', last_name: 'Other', email: '[email protected]', password: 'password'}
    ])

within db/migrate/seeds.rb

4. Update Database Tables

Run `$rails db:drop`
Run `$rails db:setup`
Run `$rails db:migrate RAILS_ENV=test`

5. Add to Devise New and Edit User (Registration) Views

Added

  <div class="field">
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name, autofocus: true, autocomplete: "first name" %>
  </div>

  <div class="field">
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name, autofocus: true, autocomplete: "last name" %>
  </div>

within app/view/registrations/...

6. Add to Application Controller

Added

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:first_name, :last_name, :email, :password) }

    devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:first_name, :last_name, :password, :current_password) }
  end

within app/controllers/application_controller.rb