[Ruby on Rails] about has_secure_password

Introduction

I summarized the has_secure_passsword method.

What is has_secure_password

has_secure_password is a method that hash (encrypts) the password.

To use this method, you need to do the following two things. -Add a gem called bcrypt. -Create a column called password_digest in the database.

How to use

Define it in the model file as follows.

user.rb


class User < ApplicationRecord
~
~
~
 has_secure_password
end

Functions that can be used

-You will be able to use two pairs of virtual attributes. (Password and password_confirmation) -The autheticate method can be used.

About two virtual attributes

Password attribute Saved in the database. (Because it is a virtual attribute, it cannot be seen on the table)

-Password_confirmation attribute Password input is confirmed for virtual attributes that are not saved in the database. This attribute is used to automatically add validation if both the password and password_confirmation attributes match.

Supplement In the password_digest attribute, put the hashed value of the value put in the password attribute.

About the autheticate method

The authenticate method is a method to check if the passwords match.

001:0> user = User.find_by(email: "example.com")

002:0> user.password_digest  #Check the hash value of the password. Make sure it is hashed.
=> "$2a$10$YmQTuuDNOszvu5yi7auOC.F4G//FGhyQSWCpghqRWQWITUYlG3XVy"

003:0>user.authenticate("not_the_right_password")  #Enter the wrong password
=> false

004:0>user.authenticate("foobar")  #Enter the correct password
=> #<User id: 1, name: "example_user", email: "[email protected]",
created_at: "2014-07-25 02:58:28", updated_at: "2014-07-25 02:58:28",
password_digest: "$2a$10$YmQTuuDNOszvu5yi7auOC.F4G//FGhyQSWCpghqRWQW...">

005:0>!!user.authenticate("foobar")  #To head"!!To get the logical value by adding "".
=> true

Recommended Posts

[Ruby on Rails] about has_secure_password
[Ruby on Rails] About bundler (for beginners)
Ruby on Rails Elementary
Ruby on Rails basics
[Ruby on Rails] About Active Record callbacks
Ruby On Rails Association
[Ruby On Rails] About RubyAws :: Sigv4 :: Errors :: MissingCredentialsError
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
About Rails 6
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
A note about the seed function of Ruby on Rails
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
About Rails routing
[Ruby on Rails] Creating an inquiry form
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
About Ruby symbols
[Ruby on Rails] 1 model CRUD (Routing Main)
About ruby ​​form
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
Ruby on Rails model creation / deletion command