Ruby on rails learning record-2020.10.07 ②

User authentication function User authentication Mechanism to confirm and access the other party -Authentication: Authentication Confirmation of identity -Permission: Give the result of Authorization authentication and use permission

devise

devise : A library that provides Rails with a user authentication mechanism

--Sign up: Save user information and encrypted password in database --User confirmation by email --Login: Authentication by email and password --Session management with cookies --User tracking: login count, date and time, IP address, etc.

  • Password reset --User lock --OmimAuth support: SNS authentication by Twitter, Facebook, etc.

Add and install devise library

gem 'devise'
$ rails g devise:install```

 <h1> Manual setting </ h1>

 Add default URL


#### **`config/environments/development.rb`**
```rb

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_caching = false

Specify root_url

config/routes.rb


root 'welcome#index'

Make a display location for flash messages

app/views/layouts/application.html.erb


<body>
  <p class="notice"><%= notice %></p>
  <p class="alert"><%= alert %></p>
  <%= yield %>
</body>

Generate view for user authentication $ rails g devise:views

devise view support --Login: app / views / devise / sessions / new.html.erb --Sign up: app / views / devise / registrations / new.html.erb --User information change: app / views / devise / registrations / edit.html.erb --Password change: app / views / devise / passwords / edit.html.erb --Email verification: app / views / devise / confirmations / new.html.erb --Password reset: app / views / devise / passwords / new.html.erb --Account unlock: app / views / devise / unlocks / new.html.erb

Creating a User model Creating a User model

rails g devise User rails db:migrate

Batch registration of initial users in devise

db/seeds.rb


User.create(email: '[email protected]', password: 'password')
User.create(email: '[email protected]', password: 'password')
User.create(email: '[email protected]', password: 'password')

$ rails db:seed

Access restrictions Add login navigation

app/views/welcome/index.html.erb


<% if user_signed_in? %>
  Logged in as <strong><%= current_user.email %></strong>.
  <%= link_to "Settings", edit_user_registration_path %> |
  <%= link_to "Logout", destroy_user_session_path, method: :delete %>
<% end %>

The "user" part of the helper method is described according to the "User" of the model name.

Forced to login page

app/controllers/welcome_controller.rb


class WelcomeController < ApplicationController
  before_action :authenticate_user!
  def index
  end
end

Session and password Session A series of access from login to logout

How to check the session </ b> For Google Chrome

  1. Call the "Settings" menu
  2. "Advanced"-"Content Settings"-"Coookies"-"All Cookies and Site Data"
    1. Search for the domain name of the web application

How to verify the encrypted password

  user = User.find(2)
  user.email
  user.encrypted_password

Recommended Posts