All posts

Reset or magic link says expired? Causes and fixes

A user writes: "the password reset link says it expired", "the magic link doesn't work", "the confirmation link says invalid", or "it sends me to localhost". The app was built with Lovable, Bolt, Cursor or Replit, or by hand, on Supabase Auth or Firebase Auth. The link usually did not expire in the way the user thinks. Vendor facts below come from the official Supabase, Firebase and Microsoft docs, checked in November 2026.

Short answer: check in this order. 1) Did an email scanner open the link first? 2) Are Site URL and redirect URLs set to your live domain? 3) Did the user click an older email, or wait too long? 4) Did they open the link in a different browser or app than the one that asked for it? 5) Are you hitting email send limits? Then fix the cause, and make the app show a helpful screen when a link fails.

1. Email scanners use up one-time links

Some mail systems open links in incoming email to check them for danger. A one-time link opened by a scanner is used up before the person sees the email. Supabase describes this on its email templates page: "Certain email providers may have spam detection or other security features that prefetch URL links from incoming emails (e.g. Safe Links in Microsoft Defender for Office 365)." It adds that the confirmation link "will be consumed instantly" and leads to a "Token has expired or is invalid" error.

Microsoft's Safe Links page says the feature provides "URL scanning and rewriting of inbound email messages during mail flow, and time-of-click verification of URLs and links in email messages". I can't tell from that page exactly what a check does to your link, so treat Supabase's description as the one to go by. Supabase's knowledge base article calls email prefetching "the most common reason" for tokens that look expired before the user can use them.

Supabase's suggested fixes on the email templates page:

  • Send a code: "Use an email OTP instead by including {{ .Token }} in the email template". The user types the code into your app, which then checks it with verifyOtp.
  • Or send a link to a page with a button: "Create your own custom email link to redirect the user to a page where they can click on a button to confirm the action". A scanner that only opens the page does not press the button.

The same page warns about email tracking: if your email provider has it on, "the links inside the Supabase email templates will be overwritten and won't perform as expected". Switch tracking off for these emails.

2. Site URL and redirect URLs are wrong

Supabase's Redirect URLs page says the redirectTo value "should match the Redirect URLs list configuration". The Site URL "defines the default redirect URL when no redirectTo is specified in the code. Change this from http://localhost:3000 to your production URL (e.g., https://example.com). This setting is critical for email confirmations and password resets."

That is the usual reason links go to localhost: the Site URL was never changed. A Supabase knowledge base article adds that for redirectTo to work "you must set the exact URL in the shown Redirect URL’s setting".

For preview deployments, the same page says wildcards "support preview URLs from providers like Netlify and Vercel". It also recommends: "we recommend setting the exact redirect URL path for your site URL in production". So use wildcards for previews and an exact URL for the live site. If you moved the app to your own domain, update both settings; see moving a Lovable or Bolt app to your own domain.

Firebase: the passing state in email actions page says: "In order to securely pass a continue URL, you need to add the domain for the URL as an authorized domain", under Security, Authentication, Settings, Authorized domains in the Firebase console. If your live or preview domain is missing there, check it first. The page doesn't say what the user sees in that case, so test it. For redirect problems with Google or Apple sign-in, see what breaks with social sign-in.

3. Link lifetime and older emails

On Supabase's passwordless email page, magic links "expire after 1 hour" by default and a user "can only request a magic link once every 60 seconds". The page says the Email OTP expiration setting "also governs the validity of Magic Links and other email links, including confirmation, password recovery, email change". It says "an expiry duration of more than 86,400 seconds (one day) is strongly discouraged".

So a user who opens a reset email the next morning will see a failure. That is by design. I could not find a Supabase or Firebase page saying that a newer email cancels an older one. I also found no Firebase page stating how long a password reset link lasts. So do not assume either: ask the user which email they clicked, and test it yourself by sending two reset emails and opening the first.

4. A different browser or app than the one that asked

Supabase's PKCE flow page says: "The code verifier is created and stored locally when the Auth flow is first initiated. That means the code exchange must be initiated on the same browser and device where the flow was started." It also says the code "has a validity of 5 minutes and can only be exchanged for an access token once".

A common case: the user asks for a link on a laptop or in an in-app browser, then taps the link in the phone's mail app, which opens another browser. The stored verifier is not there. The page doesn't describe this exact case, so take it as my reading of the rule. It is also why a code the user types in the original tab is more forgiving than a link.

5. Email send limits

Supabase's custom SMTP page says its built-in email server "is not meant for production use". It says: "Currently this value is set to 2 messages per hour", that the limit "can change without notice", and "You will need to set up a custom SMTP server to handle the delivery of messages to your users." Without it, the page says messages only go to addresses that are part of the project's team, and others fail with "Email address not authorized." If testers got links but real users did not, start here.

If the email never arrives, or lands in spam, that is a different problem: see why app emails go to spam.

How to diagnose

  1. Ask the user: which email (first or latest), which device and browser, and how many minutes after sending?
  2. Check the Supabase Auth logs. The knowledge base article says authentication logs "often show specific otp_expired error codes", and that "403 Forbidden" responses on the /verify endpoint show failed attempts. Its advice: "If you see rapid 403 errors or otp_expired messages shortly after an email is sent, but before a user could reasonably click the link, it strongly suggests prefetching."
  3. Send yourself a reset to a work address on Microsoft 365 and to a Gmail address. Click each link once, in the browser that asked for it. If only the work address fails, suspect a scanner.
  4. Repeat on your phone, opening the link from the mail app.

Prompts for Lovable, Bolt or Cursor

  • "Password reset and magic link emails fail with 'token has expired or is invalid'. Change the email templates to include the 6-digit code ({{ .Token }}) and add a screen where the user enters their email and the code, then call verifyOtp with the right type. Keep the existing login screens as they are." Or: "Change the email link to open /confirm on our site, which shows a button. Only when the user clicks the button, complete the sign-in with the confirmation URL."
  • "Links from our auth emails go to localhost. List the Site URL and Redirect URLs I must set in the Supabase dashboard for https://yourdomain.com and for the preview domain, using an exact URL for production and a wildcard only for previews. Do not change any code that is not about redirects."
  • "When a reset or sign-in link fails, don't show a blank page or a raw error. Show 'This link has expired or was already used', a short reason, and a button that sends a new one. Handle the error in the URL as well as the one from the API."

The dashboard settings are changed by you, not by the tool, so check them yourself afterwards. For wider login settings, see hardening login in an AI-built app.

Checklist

  1. Site URL is your live domain, not localhost.
  2. Redirect URLs include the live domain and, if needed, previews.
  3. Firebase: live and preview domains are in Authorized domains.
  4. Auth emails use a code, or a link to a page with a button.
  5. Email tracking is off for auth emails.
  6. The email expiry setting is what you intend.
  7. Custom SMTP is set up, so sending is not limited to the built-in server.
  8. A failed link shows a clear message and a "send a new link" button.
  9. You tested a work Microsoft 365 address, a Gmail address, and a phone mail app.

Want this fixed for you?

I finish and fix apps built with Lovable, Bolt, Cursor or Replit, including login and email links that fail for real users. Or send me the details and I'll reply within 24 hours.