From 763266420074ce2090a257683c472ff199179435 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 15 Jan 2025 16:54:29 -0600 Subject: [PATCH] Add Scaffold Auth Functionality With Rails 8 Generator as a Rails TIL --- README.md | 3 +- ...th-functionality-with-rails-8-generator.md | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 rails/scaffold-auth-functionality-with-rails-8-generator.md diff --git a/README.md b/README.md index d35c8c0..ea0d8cf 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1566 TILs and counting..._ +_1567 TILs and counting..._ See some of the other learning resources I work on: - [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators) @@ -1056,6 +1056,7 @@ See some of the other learning resources I work on: - [Run A Rake Task Programmatically](rails/run-a-rake-task-programmatically.md) - [Run Commands With Specific Rails Version](rails/run-commands-with-specific-rails-version.md) - [Run Some Code Whenever Rails Console Starts](rails/run-some-code-whenever-rails-console-starts.md) +- [Scaffold Auth Functionality With Rails 8 Generator](rails/scaffold-auth-functionality-with-rails-8-generator.md) - [Schedule Sidekiq Jobs Out Into The Future](rails/schedule-sidekiq-jobs-out-into-the-future.md) - [Secure Passwords With Rails And Bcrypt](rails/secure-passwords-with-rails-and-bcrypt.md) - [Select A Select By Selector](rails/select-a-select-by-selector.md) diff --git a/rails/scaffold-auth-functionality-with-rails-8-generator.md b/rails/scaffold-auth-functionality-with-rails-8-generator.md new file mode 100644 index 0000000..bc3d7d7 --- /dev/null +++ b/rails/scaffold-auth-functionality-with-rails-8-generator.md @@ -0,0 +1,44 @@ +# Scaffold Auth Functionality With Rails 8 Generator + +Rails 8 added a built-in generator for authentication that scaffolds the core +models, controllers, views, routes, etc. needed for a basic email/password +authentication flow. It creates a `User` model, if one doesn't already exist, +as the authenticated object. It uses the `bcrypt` gem for password hashing, +etc. + +Here is an example of what you get when running the generator on a relatively +new Rails 8 project: + +```bash +$ bin/rails generate authentication + invoke tailwindcss + create app/views/passwords/new.html.erb + create app/views/passwords/edit.html.erb + create app/views/sessions/new.html.erb + create app/models/session.rb + create app/models/user.rb + create app/models/current.rb + create app/controllers/sessions_controller.rb + create app/controllers/concerns/authentication.rb + create app/controllers/passwords_controller.rb + create app/channels/application_cable/connection.rb + create app/mailers/passwords_mailer.rb + create app/views/passwords_mailer/reset.html.erb + create app/views/passwords_mailer/reset.text.erb + create test/mailers/previews/passwords_mailer_preview.rb + insert app/controllers/application_controller.rb + route resources :passwords, param: :token + route resource :session + gsub Gemfile + bundle install --quiet + generate migration CreateUsers email_address:string!:uniq password_digest:string! --force + rails generate migration CreateUsers email_address:string!:uniq password_digest:string! --force + invoke active_record + create db/migrate/20250115224625_create_users.rb + generate migration CreateSessions user:references ip_address:string user_agent:string --force + rails generate migration CreateSessions user:references ip_address:string user_agent:string --force + invoke active_record + create db/migrate/20250115224626_create_sessions.rb +``` + +[source](https://www.bigbinary.com/blog/rails-8-introduces-a-basic-authentication-generator)