Skip to main content

bcrypt_user_auth

🔑 Use has_secure_password for User Model​

Rails provides a quick way to handle password hashing with the bcrypt gem. By adding has_secure_password to your User model, Rails will automatically add virtual password and password_confirmation attributes and handle validation and authentication methods for you.

  1. Add bcrypt to your Gemfile and run bundle install.
# Gemfile
gem 'bcrypt', '~> 3.1.7'
  1. Generate a migration to add the password_digest column to users.
rails generate migration AddPasswordDigestToUsers password_digest:string
rails db:migrate
  1. Update your User model.
class User < ApplicationRecord
has_secure_password
end