[RUBY] Implement login function in Rails simply by name and password (1)

Introduction

Implement it more simply than the email and password login used in the rails tutorial. Do not use gems such as devise.

Premise

Target

Here, log in using the name and password of the Room table.

procedure

MacBook % rails g model Room name:string

db/migrate/Time stamp.create_rooms.rb


class CreateRooms < ActiveRecord::Migration[6.0]
  def change
    create_table :rooms do |t|
      t.string :name, null: false

      t.timestamps
    end
  end
end
MacBook % rails db:migrate

app/models/room.rb


class Room < ApplicationRecord
  validates :name, presence: true, length: { maximum: 50 }
  has_secure_password
end

--Create a migration to add the password_digest column and apply the migration

MacBook % rails generate migration add_password_digest_to_rooms password_digest:string
MacBook % rails db:migrate

--Open Gemfile, uncomment gem'bcrypt' and install

Gemfile


~
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
~

--Additionally limit empty password and minimum number of characters

app/models/room.rb


class Room < ApplicationRecord
  validates :name, presence: true, length: { maximum: 50 }
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }
end

--Check if the instance can be created and saved in the database with Rails console

A terminal different from the one that started the server


MacBook % rails c  
~
irb(main):001:0> @room = Room.new(name:"tokyoroom",password:"password",password_confirmation:"password")
   (2.2ms)  SET NAMES utf8mb4,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
=> #<Room id: nil, name: "tokyoroom", created_at: nil, updated_at: nil, password_digest: [FILTERED]>
irb(main):002:0> @room.save
   (0.7ms)  BEGIN
  Room Create (3.3ms)  INSERT INTO `rooms` (`name`, `created_at`, `updated_at`, `password_digest`) VALUES ('tokyoroom', '2020-09-23 07:26:30.787094', '2020-09-23 07:26:30.787094', '$2a$12$SmCtpmRZn0M5BMtuT6BmAOKO.DMAmMOIYSXoHlxDrSbA2AQcAYnMC')
   (5.0ms)  COMMIT
=> true

Continued [2]

(https://qiita.com/yongjugithub/items/65fcdf73e42857297321)

References

https://railstutorial.jp/chapters/modeling_users?version=5.1#sec-adding_a_secure_password

Recommended Posts

Implement login function in Rails simply by name and password (1)
Implement login function in Rails simply by name and password (2)
Implement login function simply with name and password in Rails (3)
Implement simple login function in Rails
Implement application function in Rails
Implement follow function in Rails
How to implement authentication process by specifying user name and password in Spring Boot
[Ruby on Rails] Implement login function by add_token_to_users with API
Implement CSV download function in Rails
Implement user registration function and corporate registration function separately in Rails devise
[Rails] Function restrictions in devise (login / logout)
Posting function implemented by asynchronous communication in Rails
Implement star rating function using Raty in Rails6
Implement iteration in View by rendering collection [Rails]
[Rails] Implement search function
Implement markdown in Rails
How to implement guest login in 5 minutes in rails portfolio
Implement post search function in Rails application (where method)
[Rails] Implement credit card registration / deletion function in PAY.JP
Implement user follow function in Rails (I use Ajax) ②
Implement user follow function in Rails (I use Ajax) ①
Try to implement tagging function using rails and js
[Rails] Implement User search function
Implement LTI authentication in Rails
Login function implementation with rails
Implement import process in Rails
With Spring boot, password is hashed and member registration & Spring security is used to implement login function.
[Rails] Implement image posting function
[Rails] I tried to implement "Like function" using rails and js
I tried to implement Ajax processing of like function in Rails
Implement star evaluation function in Rails 6 (Webpacker is installed as standard)
Add a search function in Rails.
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
[rails] Login screen implementation in devise
Implement tagging function in form object
Implement PHP implode function in Java
Implement a contact form in Rails
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
[Ruby on Rails] How to log in with only your name and password using the gem devise
How to implement a slideshow using slick in Rails (one by one & multiple by one)
[Rails] How to log in with a name by adding a devise name column
"Teacher, I want to implement a login function in Spring" ① Hello World
How to implement search functionality in Rails
How to change app name in rails
Try to implement login function with Spring-Boot
Make a login function with Rails anyway
Implemented follow function in Rails (Ajax communication)
How to implement ranking functionality in Rails
Implement button transitions using link_to in Rails
Login function implementation by Spring Security (securityConfig)
A memo to simply create a form using only HTML and CSS in Rails 6
How to implement image posting function using Active Storage in Ruby on Rails
[Rails] Give this article to you who looked up in "devise name login"
Parse the date and time string formatted by the C asctime function in Java