How to Setup Authentication in Rails 8

1 min read Original article ↗

For years, the Ruby on Rails community has debated whether Rails should include a built-in authentication system. Historically, developers had to rely on external gems like Devise for user authentication, but the conversation has shifted.

Now, Rails 8 introduces an official generator for authentication, marking a significant evolution in the framework’s core functionality.

In this tutorial, we’ll explore how the new authentication system works in a fresh Rails 8 application. Rails is at the moment in release candidate 1. You can upgrade as follows:

gem install rails -v 8.0.0.rc1

Let’s dive in!

1. Setting Up a Rails 8 Application

To explore this new feature, we need to create a new Rails app. Here's how you can do it:

rails new test_auth
cd test_auth
code .

This sets up a Rails app called test_auth based on the beta1.

2. Creating Pages That Require Authentication

We’ll generate two pages: one that doesn’t require authentication (Home) and another that does (Dashboard). First, let’s generate a controller:

rails generate controller Home index dashboard