Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
127: Devise for Authentication
I once worked with a junior developer who spent nearly a week building a custom authentication system from scratch. He had the basic login and logout working, but then he hit the "Forgot Password" requirement. He spent another three days wrestling with secure token generation, expiration timestamps, and mailer logic. By the time he showed me the code, he was exhausted, and the implementation had a glaring security hole because he was storing the reset tokens in plain text. I told him to stop everything and install Devise. It's a rite of passage for Rails developers to try and "roll their own" auth once, only to realize that security is a massive rabbit hole you don't want to fall down alone.
Devise is essentially the industry standard for authentication in Ruby on Rails. Instead of you writing the logic for hashing passwords, managing sessions, and handling account confirmations, Devise provides a flexible "engine" that plugs into your app. It handles the heavy lifting of security best practices so you can actually spend your time building the features your users care about.
Installing the Engine
To get started, you'll add the gem to your Gemfile and run the generator. But before you run the migrations, you need to handle the environment setup. Devise requires a few things to be in place, like a root route in your config/routes.rb and a way to send emails (even if it's just the default development logger).
# Add to Gemfile
gem 'devise'
# Then run in your terminal
bundle install
rails generate devise:install
After running the install generator, you'll see a bunch of instructions in your terminal. Don't ignore them. You'll need to configure your config/environments/development.rb to define the default URL options for your mailers. If you don't, your app will crash the second Devise tries to send a password reset email.
Configuring the User Model
Once the engine is installed, you need a model to hold your user data. Devise provides a generator that creates the model and the necessary migration with all the columns you'd normally have to guess at (like encrypted_password and reset_password_token).
rails generate devise User
rails db:migrate
If you look at your user.rb model now, you'll see a line that says devise :database_authenticatable, :registerable, .... These are modules. You can pick and choose which ones you want. For example, if you want to manually approve users instead of letting anyone sign up, you'd remove :registerable. If you don't need password recovery, drop :recoverable. Most of the time, the defaults are exactly what you need, but knowing you can toggle these features is where the real power of Devise lies.
Locking Down the App
Having a User model is great, but your pages are still public. To actually protect a part of your application, you use a helper method provided by Devise called authenticate_user!. I usually wrap this in a before_action at the top of the controller.
class ProjectsController < ApplicationController
before_action :authenticate_user!
def index
@projects = current_user.projects
end
end
The moment a visitor hits that index action, Devise checks if a session exists. If not, it redirects them to the login page with a friendly flash message. Once they are logged in, Devise gives you the current_user helper, which is a lifesaver for scoping data. Instead of searching for users by an ID in the session, you just call current_user to get the actual User object for the person currently browsing.
📋 Practical Task
Securing the Secret Recipe Vault
You are building a "Secret Recipe Vault" application. Currently, any guest can see the recipes, which defeats the purpose of the vault. Your goal is to implement Devise and restrict access to the recipes.
- Add the
devisegem to your project and run the installation generator. - Configure your development mailer settings in
config/environments/development.rb. - Generate a
Usermodel using the Devise generator and run the migrations. - Create a
RecipesControllerwith anindexaction that lists all recipes. - Implement a
before_actionin theRecipesControllerto ensure that only authenticated users can view the list of recipes. - Verify that attempting to visit
/recipeswithout logging in redirects you to the sign-in page.
There are no comments for now.