Learn Enough to Be Dangerous


You have to make a choice. Choose...wisely.

Get occasional notifications about things like product discounts, blog posts, and new or updated tutorials. Unsubscribe at any time.

Quick Checkout
or Pay by Credit Card
Error processing your payment
  • You didn't choose whether or not to be added to the mailing list
Confirm
$0.00

Payments and credit card details are securely managed and protected by Learn Enough's payment processor, Stripe. More information on their site:

CART
Total
$0.00

Your Cart is Empty

$30
$300
$300
$XY
$XY
1234
Get Single Tutorial
MORE INFO

Ruby on Rails Tutorial is available as an ebook, an offline video series, and as a structured, self-paced online course. The course includes full online access to the book content, streaming videos, progress tracking, exercises, and community exercise answers.

All Access Subscription
MORE INFO

The Learn Enough All Access Subscription includes the entire Learn Enough introductory sequence and the full Ruby on Rails Tutorial. More than 2500 pages of book content and 53 hours of video that teach you to code from total beginner up to professional-grade web development.

Sign up for the course and get access to the full tutorial and streaming screencasts!

Ruby on Rails Tutorial Learn Web Development with Rails Michael Hartl

Newly updated for Rails 7, the Ruby on Rails Tutorial book and screencast series teach you how to develop and deploy real, industrial-strength web applications with Ruby on Rails, the open-source web framework that powers top websites such as GitHub, Hulu, Shopify, and Airbnb. The Ruby on Rails Tutorial book is available for purchase as an ebook (PDF, EPUB, and MOBI formats). The companion screencast series includes 14 individual lessons, one for each chapter of the Ruby on Rails Tutorial book.

Chapter 12 Password reset

Having completed account activation (and thereby verified the user’s email address) in Chapter 11, we’re now in a good position to implement password reset, and thereby handle the common case of users forgetting their passwords.1 As we’ll see, many of the steps are similar to those for account activation, and we will have several opportunities to apply the lessons learned in Chapter 11. The beginning is different, though; unlike account activation, implementing password resets requires both a change to one of our views and two new forms (to handle email and new password submission).

Before writing any code, let’s mock up the expected sequence for resetting passwords. We’ll start by adding a “forgot password” link to the sample application’s login form (Figure 12.1). The “forgot password” link will go to a page with a form that takes in an email address and sends an email containing a password reset link (Figure 12.2). The reset link will go to a form for resetting the user’s password (with confirmation), as shown in Figure 12.3.

images/figures/login_forgot_password_mockup
Figure 12.1: A mockup of a “forgot password” link.
images/figures/forgot_password_form_mockup
Figure 12.2: A mockup of the “forgot password” form.
images/figures/reset_password_form_mockup
Figure 12.3: A mockup of the reset password form.

If you followed Chapter 11, you already have a mailer for password resets, which was generated in Section 11.2 (Listing 11.6). In this section, we’ll complete the necessary preliminaries by adding a resource and data model for password resets (Section 12.1) to go along with the mailer. We’ll implement the actual password reset in Section 12.3.

In analogy with account activations, our general plan is to make a Password Resets resource, with each password reset consisting of a reset token and corresponding reset digest. The primary sequence goes like this:

  1. When a user requests a password reset, find the user by the submitted email address.
  2. If the email address exists in the database, generate a reset token and corresponding reset digest.
  3. Save the reset digest to the database, and then send an email to the user with a link containing the reset token and the user’s email address.
  4. When the user clicks the link, find the user by email address, and then authenticate the token by comparing it to the reset digest.
  5. If authenticated, present the user with the form for changing the password.

Ruby on Rails (Rails 7)

Premium Content

The Learn Enough tutorial section you have selected is premium content. You can get full access with an ebook purchase or a course subscription. If cost is a factor, please consider applying for a Learn Enough Scholarship, which includes both free and discount options.

You can send any feedback or questions to support@learnenough.com.

Get Started Now!

Course Subscriptions

$ 39
per month
Course

Full online version of the tutorial, embedded streaming videos for all sections, exercises with editable answers, progress tracking, and membership in the Learn Enough Society (community exercise answers, private chat group). Pause your subscription at any time!

$ 39
per month billed annually
All Access Subscription
All Access Subscription includes the course version of all the tutorials (streaming video, exercise answers, and progress tracking), and access to the Learn Enough Society to get help if you need it
MORE INFO
Need a little help?

Learn Enough offers a generous scholarship program to help out in case cost is a factor. We’ve already awarded over 1500 Learn Enough Scholarships to a wide variety of recipients, including students, people between jobs, and residents of countries with unfavorable exchange rates. Applications are quick, easy, and 100% confidential. Learn More

Money-Back Guarantee

All Learn Enough tutorials come with a 60-day 100% money-back guarantee. If for any reason you aren’t satisfied with any tutorial purchase, just let us know and we’ll refund your payment.

Join the Mailing List

Get occasional notifications about things like product discounts, blog posts, and new or updated tutorials. Unsubscribe at any time.