0
0
Ruby on Railsframework~30 mins

has_secure_password in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Using has_secure_password in Rails
📖 Scenario: You are building a simple user authentication system for a website. Users will have a password that needs to be stored securely.
🎯 Goal: Create a Rails model that uses has_secure_password to handle password hashing and authentication.
📋 What You'll Learn
Create a User model with a password digest column
Add has_secure_password to the User model
Create a new User instance with a password
Verify that the password is stored securely and authentication works
💡 Why This Matters
🌍 Real World
Web applications need to store user passwords securely to protect user accounts and data.
💼 Career
Understanding has_secure_password is essential for Rails developers building authentication systems.
Progress0 / 4 steps
1
Create User model with password_digest column
Create a Rails model called User with a string column named password_digest in the database migration.
Ruby on Rails
Need a hint?

Use rails generate model User password_digest:string to create the model and migration.

2
Add has_secure_password to User model
Add has_secure_password inside the User model class to enable password hashing and authentication.
Ruby on Rails
Need a hint?

Place has_secure_password inside the User class, below class User < ApplicationRecord.

3
Create a new User with a password
Write code to create a new User instance with password set to "secret123" and save it to the database.
Ruby on Rails
Need a hint?

Use User.new(password: "secret123") to create the user and then call save on it.

4
Authenticate the User with password
Use the authenticate method on the user object to check if the password "secret123" is correct.
Ruby on Rails
Need a hint?

Call user.authenticate("secret123") to verify the password.