|
| 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