Skip to main content

Password Reset

A customer resets their password on the frontend. Drupal sends the email and checks the link, and the gesso_password_reset module adds the pieces core is missing for a headless site.

How it works​

  1. The customer enters their email on the forgot password page. The Drupal Commerce plugin posts it to core's /user/password, and Drupal sends the "Password recovery" email.
  2. The link in the email opens the frontend at /account/password-reset/{uid}/{timestamp}/{hash}.
  3. The reset page checks the link with Drupal. An invalid, used or expired link shows "Link Expired".
  4. The customer enters the new password twice. Drupal saves it.
  5. The frontend signs the customer in with the new password and sends them to the home page. If the sign in fails, it sends them to /account/login.

The module does not log anyone in or start a session. The confirm endpoint only saves the password and returns the account name, and the frontend signs in with the normal login.

Drupal setup​

  1. Install and enable acromedia/gesso_password_reset:

    lando composer require acromedia/gesso_password_reset
    lando drush en gesso_password_reset
  2. At /admin/config/people/gesso-password-reset, set the frontend URL and path. The path defaults to /account/password-reset. If the URL is empty, the module uses [site:frontend_url] from gesso_headless.

  3. At /admin/config/people/accounts, put the token in the "Password recovery" email so it links to the frontend:

    [user:frontend-password-reset-url]
  4. Make sure Drupal can send mail. See Sending Email.

If neither a frontend URL nor [site:frontend_url] is set, the token is removed from the email and an error is logged.

The link works once and expires after 24 hours by default (password_reset_timeout in user.settings).

Password rules​

The new password goes through Drupal's own validation. A rejected password shows Drupal's message on the page and the link stays valid.

Rules that a module adds to the user form only, such as Password Policy, do not run on a reset. Those modules check the password in the form, and the reset does not use the form.

Attempt limits​

Failed attempts are limited per IP address: 20 link checks and 5 password changes per hour by default. The settings page changes both. If the frontend calls Drupal through a proxy or from its server, Drupal must see the visitor's IP. Otherwise every visitor shares one counter. List only your proxy's addresses in reverse_proxy_addresses. Trusting $_SERVER['REMOTE_ADDR'] lets any caller set their own IP with X-Forwarded-For and get around the limits.

Frontend setup​

The @acromedia/gesso-next, @acromedia/gesso-commerce and @acromedia/gesso-drupal-commerce releases that include the reset page and passwordResetValidate / passwordResetConfirm are required.

New sites from create-nextjs with Drupal Commerce get the route. An existing site adds one of these files.

App Router, src/app/account/password-reset/[...slug]/page.tsx:

import React from 'react';
import type { Metadata } from 'next';
import { ResetPasswordPage } from '@acromedia/gesso-next/app-router';

// The emailed link is a credential, so keep it out of the Referer header.
export const metadata: Metadata = { referrer: 'no-referrer' };

const ResetPasswordPageHandler = () => {
return <ResetPasswordPage />;
};

export default ResetPasswordPageHandler;

Pages Router, src/pages/account/password-reset/[...slug].tsx:

import React from 'react';
import Head from 'next/head';
import { ResetPasswordPage } from '@acromedia/gesso-next';
import commerce from '../../../commerce';

const ResetPasswordPageHandler = () => {
return (
<>
{/* The emailed link is a credential, so keep it out of the Referer header. */}
<Head>
<meta name="referrer" content="no-referrer" />
</Head>
<ResetPasswordPage commerce={commerce} />
</>
);
};

export default ResetPasswordPageHandler;

The route path must match the path set in the Drupal module.

Moving from rest_password​

Before this release, @acromedia/gesso-drupal-commerce sent a temporary password through rest_password's /user/lost-password. That flow is gone.

  • Install gesso_password_reset and update the "Password recovery" email as above.
  • Upgrade the packages and add the reset route from Frontend setup.
  • passwordReset, which used the temporary password, is deprecated. Use passwordResetValidate and passwordResetConfirm.
  • The temporary password form on the account details page (?resetPassword=true) is removed.

Troubleshooting​

  • "Request Sent!" but no email: Drupal answers the same for an address it doesn't know, so the page never reveals whether an account exists. Check the address belongs to a user, then check mail delivery.
  • The email links to Drupal, not the frontend: the "Password recovery" email still has core's [user:one-time-login-url]. Replace it with the token above.
  • The link is empty: no frontend URL is set. Check the settings page and the Drupal log.
  • "Link Expired": the link was already used to change the password, the customer has signed in since the email was sent, the account is blocked, or the link is older than the reset timeout.
  • The link opens a 404: the path in the module settings doesn't match the frontend route.
  • Too many attempts message for everyone: Drupal sees the proxy's IP, not the visitor's. See the attempt limits above.