ruby
49 lines · 9 steps
Password resets with signed global IDs in Rails
A password reset flow that encodes the user in a signed, expiring token instead of storing reset records in the database.
Explained by
highlit
1class PasswordResetsController < ApplicationController
2 RESET_PURPOSE = :password_reset
3 RESET_EXPIRY = 15.minutes
4
5 def create
6 user = User.find_by(email: params[:email].to_s.downcase.strip)
7
8 if user
9 sgid = user.to_sgid(expires_in: RESET_EXPIRY, for: RESET_PURPOSE)
10 reset_url = edit_password_reset_url(token: sgid.to_s)
11 UserMailer.with(user: user, reset_url: reset_url).password_reset.deliver_later
12 end
13
14 redirect_to new_session_path, notice: "If that account exists, we've emailed a reset link."
15 end
16
17 def edit
18 @user = authenticated_user
19 return if @user
20
21 redirect_to new_password_reset_path, alert: "That reset link is invalid or has expired."
22 end
23
24 def update
25 @user = authenticated_user
26
27 unless @user
28 redirect_to new_password_reset_path, alert: "That reset link is invalid or has expired."
29 return
30 end
31
32 if @user.update(password_params)
33 reset_session
34 redirect_to new_session_path, notice: "Your password has been reset. Please sign in."
35 else
36 render :edit, status: :unprocessable_entity
37 end
38 end
39
40 private
41
42 def authenticated_user
43 GlobalID::Locator.locate_signed(params[:token], for: RESET_PURPOSE)
44 end
45
46 def password_params
47 params.require(:user).permit(:password, :password_confirmation)
48 end
49end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Signed global IDs let you carry a verified, expiring user reference in a URL without a database column.
- 2Returning the same generic response whether or not the account exists prevents user enumeration.
- 3Scoping tokens with a purpose stops a token minted for one action from being replayed for another.
Related explainers
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
ruby
class ApplicationController < ActionController::Base EXPERIMENTS = { checkout_button_color: %w[control blue green], onboarding_flow: %w[control streamlined]
How A/B test cohorts are assigned in Rails
a-b-testing
cookies
hashing
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/password-resets-with-signed-global-ids-in-rails-explained-ruby-be40/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.