Forgot-password flow for the web and mobile apps
Ruby · Rails · intermediate · modification
Adds the forgot-password flow the web and mobile apps call. `config/routes.rb` gains `resource :password_reset, only: %i[create update]` under `namespace :api`. `User` turns off the reset token `has_secure_password` adds by default and declares it itself with `generates_token_for :password_reset, expires_in: 15.minutes` over the end of the password salt, so purpose, lifetime and what invalidates it sit in one place; `User#reset_password` saves the new password and deletes the account's sessions in one transaction. `Api::PasswordResetsController#create` enqueues `PasswordResetEmailJob` with the address and answers 202 with one fixed message, behind `rate_limit to: 5, within: 15.minutes`; `#update` reads the three fields with `params.expect`, finds the user with `find_by_token_for` and answers 204, or 422 for an unusable token or a rejected password. The job looks the account up and sends `PasswordsMailer#reset` (`reset.text.erb`), with `log_arguments` off. `config/environments/test.rb` gives controllers a memory cache store so the rate limit counts in tests, and `test_helper.rb` clears it before every integration test. `password_resets_controller_test.rb` adds integration tests for both endpoints and the email.
Ruby 4.0, Rails 8.1 JSON API (`ActionController::API`) behind Tasklane's single-page web app and mobile apps. SQLite in development and test, PostgreSQL 16 in production; nothing here depends on the database. `bcrypt` is already in the Gemfile for `has_secure_password`. Existing and unchanged: `users` (`name`, `email_address` unique and `NOT NULL`, `password_digest` `NOT NULL`); `Session` (`belongs_to :user`, `has_secure_token :token`, no callbacks); `Api::BaseController` authenticates the `Authorization: Bearer` token against `Session`, sets `Current.session` and answers 401 otherwise; `Api::SessionsController` signs in (`POST /api/session`, `rate_limit to: 10, within: 3.minutes`) and out. Clients send JSON and bearer tokens and no cookies, so CSRF protection does not apply. Production: Puma on several app servers behind a load balancer, with `trusted_proxies` set so `request.remote_ip` is the client's address; `config.cache_store = :solid_cache_store` (Solid Cache on the primary database), shared by every server, and `config.action_controller.cache_store` is not set there, so controllers use that store; Active Job on Solid Queue with no retries configured (a failed job stays in Solid Queue's failed jobs, and the user can ask again); mail through the company SMTP relay, which accepts every recipient and bounces undeliverable mail later; production logs at the `info` level (`RAILS_LOG_LEVEL` is not set). Tokens are signed with the app's `secret_key_base`, and `filter_parameters` is Rails' default list (including `:passw`, `:email` and `:token`). `config.x.web_app_url` is `https://app.tasklane.com` in every environment; the reset page there ships separately, reads the token from the link and sends the `PATCH`. Tests are Minitest integration tests with inline records; the test environment keeps `config.cache_store = :null_store`, delivers mail with `:test`, and Rails' `ActiveJob::TestHelper` gives tests the test queue adapter.
Requirements
- `POST /api/password_reset` (new, no sign-in) takes `email_address` and answers 202 with the same JSON message whether or not an account uses that address, so the endpoint does not reveal which addresses have accounts; a missing, `null` or blank (empty or only whitespace) `email_address`, an array or an object answers 400, and any other JSON value (a number, `true`) is read as text, like an unknown address. The address is matched the way the `User` model already normalizes it (surrounding spaces removed, lower case). When an account matches, its owner gets an email with a link to `https://app.tasklane.com/reset-password?token=<token>`.
- The link works for 15 minutes after the email is sent. It stops working as soon as the account's password changes, through this reset or any other way, so a link that has been used cannot be used again, and other links sent earlier stop working with it.
- `PATCH /api/password_reset` (new, no sign-in) takes `token`, `password` and `password_confirmation`. With a valid token it sets the new password, ends every session of that account and answers 204. A token that is not blank but is malformed, tampered with, expired or already used answers 422 with an error message. A new password that fails the existing rules (at least 12 characters, at most 72 bytes, the confirmation must match) answers 422 with the errors and changes nothing: the old password, the sessions and the link keep working. A missing, `null` or blank (empty or only whitespace) field, an array or an object answers 400; any other JSON value (a number, `true`) is read as text, so a password that is not a string is checked by the same rules. The new password and the end of the sessions are saved together or not at all.
- `POST /api/password_reset` is rate limited per client IP address with Rails' `rate_limit`: at most 5 requests in a 15-minute window, and a request over the limit answers 429. `PATCH` is not rate limited in this PR.
- The email is sent by a background job, so the request does not wait for the mail server, and the job's log lines do not contain the email address. Two `PATCH` requests with the same valid token at the same moment may both be applied (the last password wins); both come from whoever holds the link, and this is accepted.
Files touched
- config/routes.rb
- app/models/user.rb
- app/controllers/api/password_resets_controller.rb
- app/jobs/password_reset_email_job.rb
- app/mailers/passwords_mailer.rb
- app/views/passwords_mailer/reset.text.erb
- config/environments/test.rb
- test/test_helper.rb
- test/controllers/api/password_resets_controller_test.rb