LoginSignup
0
0

More than 3 years have passed since last update.

【Rails】has_secure_password

Posted at

has_secure_password

user.rb
class User < ApplicationRecord
  .
  .
  .
  has_secure_password
end

上記のようにユーザモデルに定義すると機能します。

機能

Railsのメソッド、下記のような機能を持っています。

・セキュアなハッシュ化されたパスワードをpassword_digestカラムに保存する。
・passwordとpassword_confirmationという2つの仮想的な属性を追加する。
validations: trueを記載することで存在性と値が一致するかどうかのバリデーションが追加される。
(「password」は存在しているか、「password」と「password_confirmation」は同じか)
・authenticateメソッドが使えるようになる。
(引数の文字列がパスワードと一致するとUserオブジェクトを、間違っているとfalseを返すメソッド)

前提条件

モデル内にpassword_digestという属性が含まれていないとhas_secure_passwordの機能は動作しない。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0