0
0
Ruby on Railsframework~30 mins

Login and logout flow in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Login and logout flow
📖 Scenario: You are building a simple web app where users can log in and log out. This helps users access their personal dashboard securely.
🎯 Goal: Create a basic login and logout flow using Rails. You will set up user data, configure session handling, implement login logic, and add logout functionality.
📋 What You'll Learn
Create a hash called users with usernames as keys and passwords as values
Create a variable called session as an empty hash to store login state
Write a method login that takes username and password, checks credentials, and sets session[:user] if valid
Write a method logout that clears the session[:user] to log out the user
💡 Why This Matters
🌍 Real World
Login and logout flows are essential for any web app that needs user accounts and personalized access.
💼 Career
Understanding session management and authentication basics is key for backend and full-stack developers working with Rails.
Progress0 / 4 steps
1
DATA SETUP: Create user data
Create a hash called users with these exact entries: 'alice' => 'password123', 'bob' => 'secure456', and 'carol' => 'qwerty789'.
Ruby on Rails
Need a hint?

Use a Ruby hash with string keys and string values for passwords.

2
CONFIGURATION: Create session hash
Create an empty hash called session to store the login state.
Ruby on Rails
Need a hint?

Just create an empty hash named session.

3
CORE LOGIC: Implement login method
Write a method called login that takes username and password. Inside, check if users[username] equals password. If yes, set session[:user] = username. Otherwise, do nothing.
Ruby on Rails
Need a hint?

Use an if statement to compare the password and assign session[:user].

4
COMPLETION: Implement logout method
Write a method called logout that removes the :user key from the session hash to log out the user.
Ruby on Rails
Need a hint?

Use session.delete(:user) inside the logout method.