[RUBY] Creating a user authentication function using devise

Have your application load "devise"

Gemfile


:
gem 'devise' #add to

terminal


$ bundle install

Create models, views and controllers with devise commands

terminal


$ rails g devise:install
$ rails g devise User name:string #Creating a model
$ rails db:migrate
$ rails g devise:views users #Create view
$ rails g devise:controllers users #Creating a controller

Since the name column does not exist by default in the model created with rails g devise" model name ", create the column at the same time as described above, or describe it in the migration file and reflect it in the table.

・ Created model

app/models/user.rb


class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
end

Default features

Routing changes

config/routes.rb


...Edit
devise_for :users #Declaration to include users in the URL when using the devise feature
↓
devise_for :users, controllers: { 
  sessions: 'users/sessions', #Use the sessions controller under the users directory
  registrations: 'users/registrations' #Use the registrations controller under the users directory
}

The above controllers: {} specification changes the routing settings as follows:

terminal


devise/sessions#create
↓
users/sessions#create

Edit view page

Added name input form to new registration screen

erb:app/views/users/registrations/new.html.erb


... +Add part of
+ <div class="field"> 
+  <%= f.label :name %><br />
+  <%= f.text_field :name, autofocus: true, autocomplete: "name" %>
+ </div>

<div class="field">
  <%= f.label :email %><br />
  <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
:

Changed the login screen input form from email to name ** This will change to email login authentication **

erb:app/views/users/sessions/new.html.erb


...Edit
:
 <div class="field">
   <%= f.label :email %><br />
   <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
 </div>
↓
 <div class="field">
   <%= f.label :name %><br />
   <%= f.text_field :name, autofocus: true, autocomplete: "name" %>
 </div>
:

Edit controller

app/controllers/application_controller.rb


...add to
before_action :configure_permitted_parameters,if: :devise_controller? 

protected

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up,keys:[:name])
end

When the devise usage functions (user registration, login authentication, etc.) are used, configure_permitted_parameters is executed before that.

devise_parameter_sanitizer.permit(:sign_up,keys[:name]) = Allow data manipulation of user name (name) at the time of user registration (sign_up)

Similar functionality to Strong Parameters ** · private = See only within your controller ** ** · protected = Can be referenced by other called controllers **

Implementation of logout function

erb:app/views/layouts/application.html.erb



...+Add part
  <body>
+   <% if user_signed_in? %>
+     <%= link_to "Logout", destroy_user_session_path, method: :delete %>
+   <% else %>
+     <%= link_to "sign up", new_user_registration_path %>
+     <%= link_to "Login", new_user_session_path %>
+   <% end %>
:

that's all

** We would appreciate it if you could point out any mistakes in the description. We are looking forward to hearing from you. ** **

Recommended Posts

Creating a user authentication function using devise
Implementation of user authentication function using devise (1)
Implementation of user authentication function using devise (3)
[Implementation procedure] Create a user authentication function using sorcery in Rails
Create authentication function in Rails application using devise
Implement user edit / update function without using devise
Creating a calendar using Ruby
Create a login authentication screen using the session function
Memorandum [Rails] User authentication Devise
User evaluation using the like function
Create a filtering function using acts-as-taggable-on
Create a login function using Swift's Optional
Use [Rails] devise Guest user function (for portfolio)
Tag function using acts-as-taggable-on on Devise My memo
devise user registration
[Rails] I made a draft function using enum
[Rails] Creating a breadcrumb trail using Gem gretel
[Note] Summary of rails login function using devise ①
[For beginners] Procedure for creating a controller using rails
When I created a user my page, the user editing function of devise became strange.
How to update devise user information without a password
[Rails] Google, Twitter, Facebook authentication using Devise and Omniauth
A memorandum for creating an extended logger using org.slf4j.Logger
Flash message using devise
Creating a local repository
Creating a test case
Introduction of user authentication
Creating a docker host on AWS using Docker Machine (personal memorandum)
Creating a 2021 weekly calendar (refill for personal organizer) using Ruby
Procedure for publishing an application using AWS (4) Creating a database
Implement user registration function and corporate registration function separately in Rails devise