How to Add Password Reset to an AI-Built App
Password reset is the easiest feature to get subtly wrong. Here are the rules from OWASP, a Supabase example, and a review checklist for AI-written code.
To add password reset to an AI-built app safely, return the same message whether or not the email has an account, email a random single-use token that expires quickly, rate-limit the request, and end the user's other sessions once the password changes. Each of those four rules blocks a specific attack, and a generated flow that skips any one of them is exploitable.
Password reset is a good feature to understand even if you never write it by hand, because it is the one flow where a small shortcut turns directly into account takeover. This guide uses the OWASP Forgot Password Cheat Sheet as the standard, shows a working example, and ends with a checklist you can run against whatever your AI builder produced.
Add password reset with your auth provider
If your app uses a hosted auth service, do not build reset yourself. Supabase, for instance, documents resetPasswordForEmail, which sends a reset email to an address and supports the PKCE flow. The pattern in its reference has three steps:
// 1. Ask for the email
await supabase.auth.resetPasswordForEmail(email, {
redirectTo: 'https://yourapp.com/update-password',
})
// 2. On the redirect page, wait for the recovery event
supabase.auth.onAuthStateChange(async (event, session) => {
if (event === 'PASSWORD_RECOVERY') {
// show the "choose a new password" form
}
})
// 3. Save the new password
await supabase.auth.updateUser({ password: newPassword })The redirectTo value sends the person back to your app from the email link, and the PASSWORD_RECOVERY event is what your code listens for to know the visitor arrived through a reset link. These are taken from the Supabase reference. A provider handles token generation, expiry and delivery, which are the hardest parts. If you are choosing between sign-in methods first, the magic link guide shows a flow that removes passwords, and with them reset, altogether.
You still own the parts around it: the request page, the new-password page, and the settings that decide where the link is allowed to land.
Password reset belongs to the wider account system described in building an app with AI, so read that first if you are starting from nothing.
The rules that matter, from OWASP
The OWASP Forgot Password Cheat Sheet is short and worth reading in full. These are the requirements that most often fail in generated code.
Rule | What it protects against |
|---|---|
Return a consistent message for existing and non-existing accounts | Attackers using the form to learn who has an account |
Keep response time uniform | The same leak, through a slow path for real accounts |
Token generated with a cryptographically safe random source and long enough to resist brute force | Guessable reset links |
Token is single use and expires | Old emails becoming permanent keys |
Rate limiting on requests and on token attempts | Flooding inboxes and brute-forcing tokens |
Reset URL uses HTTPS and does not trust the Host header | Links that point at an attacker's domain |
Reset page sets Referrer-Policy to no-referrer | The token leaking to third-party scripts through the referrer |
Existing sessions are invalidated or the user is offered the choice | A thief staying logged in after the owner resets |
A confirmation email that never contains the password | Silent takeover with no warning to the owner |
Do not lock accounts after forgotten-password attempts | Attackers locking real users out on purpose |
Five bugs AI builders commonly introduce
These are patterns that show up when a flow is generated from a one-line prompt. Check for each.
"No account found with that email." Friendly, and a leak. The message must be the same either way, something like "If that address has an account, we have sent a link."
Tokens built from a timestamp or user ID. Anything predictable is guessable. The token has to come from a secure random generator, never a hash of known values.
A token that works more than once or never expires. Look for a
used_atcolumn or equivalent, and anexpires_atcheck on the server.A link built from the request's host header. If the app copies the host from the incoming request into the email, an attacker can send a request with their own host and get a victim to click a link to a page they control. Hard-code the site URL or check it against an allow-list.
Logging the user straight in after the reset. OWASP's advice is to have the user log in normally afterwards, not auto-login, and to deal with other sessions.
If you let an AI coding agent edit authentication code, review the diff carefully. The guide on letting an agent touch your auth code explains which changes deserve a second pair of eyes.
If you build the tokens yourself
Skip this section if your provider handles it. Otherwise the minimum design is a table where you store only a hash of the token, never the token itself, so a database leak does not hand out working reset links.
create table password_resets (
id uuid primary key default gen_random_uuid(),
user_id uuid not null references users(id),
token_hash text not null unique,
expires_at timestamptz not null,
used_at timestamptz,
created_at timestamptz default now()
);The flow is: generate 32 random bytes, email the token in the link, store its hash with an short expiry such as 30 minutes, and when the link is used, hash the incoming token, look it up, check it is unexpired and unused, then set used_at in the same transaction that changes the password. OWASP only asks for an appropriate period, so the number is your call. Pick the shortest window your users can live with.
Rate limits and email delivery
The request endpoint sends email, so anyone can use it to spam a victim. Limit requests per address and per IP, and give the same response when you throttle. The walkthrough for adding rate limiting covers the mechanics. Sending itself needs a working mail setup with a verified sender, which is covered in adding email sending to an AI-built app. A reset that lands in spam looks like a broken feature to the customer.
A review checklist to run on your own app
Request a reset for an address that exists and one that does not. Compare the message and, roughly, the response time. They should match.
Use a reset link twice. The second use must fail.
Wait past the expiry and use the link. It must fail.
Request ten resets in a minute. You should be throttled.
Open the reset link in a browser and check the response headers for a no-referrer policy.
Reset while logged in on a second device. Confirm that device is signed out, or that you were offered the choice.
Check that the confirmation email arrives and contains no password.
Frequently asked questions
How long should a password reset link last?
Long enough to open an email and short enough that an old message is useless. Many apps use somewhere between 15 minutes and an hour. OWASP asks only that the token be single use and expire after an appropriate period.
Should I email a temporary password instead?
No. A temporary password sitting in an inbox is a long-lived secret, and it trains users to type passwords from email. A single-use link is the standard approach.
Is it safe to tell the user the email is not registered?
It is not recommended. It lets anyone check which addresses have accounts. Use the same wording for both cases, and keep response timing similar.
Do I need password reset if I use social login or magic links?
Not for those accounts, because there is no password. If you also allow passwords, you still need reset for them. See the guide on adding social login for that side.
How did this land?
About the author

Staff Engineer, Platform
Carlo works on the platform that turns prompts into running apps. He writes the engineering deep dives and the changelog notes worth reading.


