Skip to content

Commit 7632664

Browse files
committed
Add Scaffold Auth Functionality With Rails 8 Generator as a Rails TIL
1 parent 872a1d2 commit 7632664

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1566 TILs and counting..._
13+
_1567 TILs and counting..._
1414

1515
See some of the other learning resources I work on:
1616
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
@@ -1056,6 +1056,7 @@ See some of the other learning resources I work on:
10561056
- [Run A Rake Task Programmatically](rails/run-a-rake-task-programmatically.md)
10571057
- [Run Commands With Specific Rails Version](rails/run-commands-with-specific-rails-version.md)
10581058
- [Run Some Code Whenever Rails Console Starts](rails/run-some-code-whenever-rails-console-starts.md)
1059+
- [Scaffold Auth Functionality With Rails 8 Generator](rails/scaffold-auth-functionality-with-rails-8-generator.md)
10591060
- [Schedule Sidekiq Jobs Out Into The Future](rails/schedule-sidekiq-jobs-out-into-the-future.md)
10601061
- [Secure Passwords With Rails And Bcrypt](rails/secure-passwords-with-rails-and-bcrypt.md)
10611062
- [Select A Select By Selector](rails/select-a-select-by-selector.md)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Scaffold Auth Functionality With Rails 8 Generator
2+
3+
Rails 8 added a built-in generator for authentication that scaffolds the core
4+
models, controllers, views, routes, etc. needed for a basic email/password
5+
authentication flow. It creates a `User` model, if one doesn't already exist,
6+
as the authenticated object. It uses the `bcrypt` gem for password hashing,
7+
etc.
8+
9+
Here is an example of what you get when running the generator on a relatively
10+
new Rails 8 project:
11+
12+
```bash
13+
$ bin/rails generate authentication
14+
invoke tailwindcss
15+
create app/views/passwords/new.html.erb
16+
create app/views/passwords/edit.html.erb
17+
create app/views/sessions/new.html.erb
18+
create app/models/session.rb
19+
create app/models/user.rb
20+
create app/models/current.rb
21+
create app/controllers/sessions_controller.rb
22+
create app/controllers/concerns/authentication.rb
23+
create app/controllers/passwords_controller.rb
24+
create app/channels/application_cable/connection.rb
25+
create app/mailers/passwords_mailer.rb
26+
create app/views/passwords_mailer/reset.html.erb
27+
create app/views/passwords_mailer/reset.text.erb
28+
create test/mailers/previews/passwords_mailer_preview.rb
29+
insert app/controllers/application_controller.rb
30+
route resources :passwords, param: :token
31+
route resource :session
32+
gsub Gemfile
33+
bundle install --quiet
34+
generate migration CreateUsers email_address:string!:uniq password_digest:string! --force
35+
rails generate migration CreateUsers email_address:string!:uniq password_digest:string! --force
36+
invoke active_record
37+
create db/migrate/20250115224625_create_users.rb
38+
generate migration CreateSessions user:references ip_address:string user_agent:string --force
39+
rails generate migration CreateSessions user:references ip_address:string user_agent:string --force
40+
invoke active_record
41+
create db/migrate/20250115224626_create_sessions.rb
42+
```
43+
44+
[source](https://www.bigbinary.com/blog/rails-8-introduces-a-basic-authentication-generator)

0 commit comments

Comments
 (0)