1.modelにhas_secure_passwordを追加
class User < ApplicationRecord
.
.
has_secure_password
end
2.modelにpassword_digestという属性を追加
rails g migration add_password_digest_to_users password_digest:string
class AddPasswordDigestToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :password_digest, :string
end
end
rails db:migrate
3.bcryptを追加
source 'https://rubygems.org'
gem 'rails', '5.1.6'
gem 'bcrypt', '3.1.12'
.
.
bundle install
4.passwordの最小文字数を設定
validates :password, length: { minimum: 6 }
5.ユーザーの作成と認証
User.create(name: "foo", email: "bar@example.com", password: "baz", password_confirmation: "baz")
user.authenticate("baz")