Implementation of user authentication function using devise (3)

New user registration

To save the information sent from the view in the User table Specify the instance variable and url of model in form_with.

registrations/new.html.erb


<%= form_with model:@user, url:"/users", local: true do |f| %>
 #abridgement
<% end %>

The @user specified in model: here is an instance variable based on the User model, and is declared in the devise controller. The devise controller is usually invisible, but it seems that you can generate it with the following command and customize it.

$ rails g devise:controllers users

The part of URI Pattern set in the following routing is specified in url :.

Prefix Verb   URI Pattern             Controller#Action                                                                 
       POST   /users(.:format)        devise/registrations#create

Also, if you want to save the information in a column other than email and password, you must allow saving in the column with the following description. In the example below, keys: [: username] is allowed to save to the username column during sign_up.

application_controller.rb


class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

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

Reference: https://github.com/heartcombo/devise#strong-parameters

Log out

To implement the logout function, prepare a logout link on the view file as shown below.

<%= link_to 'Log out', destroy_user_session_path, method: :delete, class: "logout" %>

It is OK if you set path and method: in the same way as the routing setting.

Prefix               Verb   URI Pattern                Controller#Action
destroy_user_session DELETE /users/sign_out(.:format)  devise/sessions#destroy

Login

To implement the login function, prepare a login link on the view file as shown below.

<%= link_to 'Login', new_user_session_path, class: "login" %>

In the form of the view that prompts you to enter login information, write as follows.

sessions/new.html.erb


<%= form_with model:@user, url:user_session_path, class: 'registration-main', 
  #abridgement
<% end %>

Send request url: As you can see, When the login link is clicked, the new action of devise / sessions is executed and After entering the login information, when submitted, the create action of devise / sessions will be executed and you can log in.

Prefix           Verb   URI Pattern                Controller#Action
new_user_session GET    /users/sign_in(.:format)   devise/sessions#new
    user_session POST   /users/sign_in(.:format)   devise/sessions#create

Although it is a brief explanation, the minimum function of the user authentication function using devise has been implemented. Thank you for visiting.

Return to (2) Return to (1)

Recommended Posts

Implementation of user authentication function using devise (2)
Implementation of user authentication function using devise (1)
Implementation of user authentication function using devise (3)
Creating a user authentication function using devise
[Rails] Implementation of user withdrawal function
[Rails] Implementation of new registration function in wizard format using devise
[Implementation procedure] Create a user authentication function using sorcery in Rails
Implementation of search function
Implementation of pagination function
Introduction of user authentication
[Rails] Implementation of search function using gem's ransack
[Rails 6] Implementation of inquiry function using Action Mailer
Create authentication function in Rails application using devise
[Rails] Implementation of image enlargement function using lightbox2
[Note] Summary of rails login function using devise ①
Implementation of Google Sign-In using Google OAuth 2.0 authentication (server edition)
Implementation of Ruby on Rails login function (devise edition)
Implementation of sequential search function
Implementation of like function (Ajax)
[Rails 6] Implementation of search function
Implementation of image preview function
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of category function
Implementation of category pull-down function
[Rails] Implementation of tutorial function
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
[Rails] Implementation of like function
Memorandum [Rails] User authentication Devise
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
[Rails] Implementation of tagging function using intermediate table (without Gem)
[Rails] Implementation of user logic deletion
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
User evaluation using the like function
Implement user management functionality using Devise
[Rails] About implementation of like function
[Rails] Implementation of CSV export function
Implementation of validation using regular expressions
Implementation of like function in Java
[Rails] Sign-up function using devise error number of arguments (given 0, expected 1)
[Rails 6] Implementation of new registration function by SNS authentication (Facebook, Google)
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of tabs using TabLayout and ViewPager
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails] I will explain the implementation procedure of the follow function using form_with.
How to implement the email authentication function at the time of user registration
Implementation of Ruby on Rails login function (Session)
Use [Rails] devise Guest user function (for portfolio)
DM function implementation
[FCM] Implementation of message transmission using FCM + Spring boot
Today's learning summary: With user management function implementation
devise user registration
[Rails] Implementation of retweet function in SNS application
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
[Rails] Implementation of batch processing using whenever (gem)
Implementation of search function Learning memo (portfolio creation)