--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.
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
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
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
How to check the session </ b> For Google Chrome
How to verify the encrypted password
user = User.find(2)
user.email
user.encrypted_password
Recommended Posts