User Sign-In with the Devise Gem
Master User Authentication with Rails Devise Gem
What You'll Build
User Authentication System
Implement secure sign-in and sign-out functionality using the industry-standard Devise gem for Rails applications.
Flash Message Display
Create user feedback system to display success messages, error alerts, and status updates across page transitions.
Dynamic Navigation
Build conditional navigation that shows different options based on user authentication status.
Quick Setup Process
Clean Environment
Close files, navigate to Class Files folder, and delete existing nutty directory
Clone Repository
Use Git clone to download fresh copy of That Nutty Guy project repository
Checkout Branch
Switch to B2 branch to match the end state of previous exercises
Install Dependencies
Run bundle and yarn install to set up all required gems and JavaScript packages
Devise Installation Timeline
Add Gem to Gemfile
Include devise gem with descriptive comment
Run Bundle Install
Install gem and restart Rails server
Generate Devise Config
Run devise:install generator for initial setup
Create Customer Model
Generate and migrate Customer authentication model
This tutorial uses 'Customer' instead of 'User' to distinguish from the upcoming 'AdminUser' model. This separation provides better organization for sites with multiple user types.
Sign-in Implementation Checklist
Show different links based on customer_signed_in? status
Use new_customer_session_path for GET requests
Use destroy_customer_session_path with DELETE method
Use rails routes command to verify correct paths and HTTP verbs
HTTP Verbs for Authentication Routes
| Feature | Sign In | Sign Out |
|---|---|---|
| Route Helper | new_customer_session_path | destroy_customer_session_path |
| HTTP Verb | GET (default) | DELETE (specified) |
| Method Required | No | method: :delete |
Flash Message Types
Alert Messages
Display error messages like incorrect username or password using flash[:alert] with alert CSS class.
Notice Messages
Show success confirmations like successful login or logout using flash[:notice] with notice CSS class.
Testing Your Authentication System
Create New Account
Click Sign In, then Sign Up link to create account with email and password
Verify Success Messages
Confirm green success banner appears after successful signup
Test Sign Out
Click Sign Out link and verify logout confirmation message displays
Test Sign In
Use existing credentials to sign back in and verify authentication works
Key Takeaways