4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bcryptでパスワード取り扱い

Posted at
gem 'bcrypt', '~>3.1.7'
def change
  create_table :users do |t|
    t.string :password_digest,  null: false
  end
end
app/models/user.rb
class User < ActiveRecord::Base
  # SecurePassword有効
  has_secure_password
end
# ユーザ作成
User.create password: "パスワード"
=> #<User>

# ユーザ検索
user = User.find 1

# ユーザ認証(成功)
user.authenticate "パスワード"
=> #<User>

# ユーザ認証(失敗)
user.authenticate "異なるパスワード"
=> false

# 1発で検索
User.find_by(id: 1).try(:authenticate, "パスワード")
=> #<User>
4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?