Supabase "Email link is invalid or has expired": why the link dies before the user clicks it
By AI App Medic · Updated · Supabase · Auth · Magic link · Lovable
A user signs up, gets the email, clicks the button, and lands on your homepage with this in the address bar:
https://yourapp.com/?error=access_denied&error_code=otp_expired&error_description=Email+link+is+invalid+or+has+expired
Depending on the auth flow, the same fields also appear after a # in the URL. If your code calls verifyOtp or visits the verify endpoint directly, the failure shows as an HTTP 403 with a message like Token has expired or is invalid. The user did nothing wrong. They clicked a fresh link and it was already dead.
This is one of the most common support emails for apps built with Lovable, Bolt and Cursor, because the default Supabase email templates all use a one time link. Here is why the link dies and how to fix it properly.
What otp_expired actually means
Every confirmation, magic link, password recovery, email change and invite link carries a one time token. Supabase accepts it once. After that, and after the expiry time, the same link returns otp_expired. The message covers both a token that is past its time and a token that was already used, so the text does not tell you which one happened. The error=access_denied part is only the OAuth label Supabase attaches. It does not mean an admin blocked the user.
Step 1: find out whether a scanner used the link
Open the Supabase dashboard, go to Logs, and look at the Auth logs for otp_expired and the API logs for 403 responses on /auth/v1/verify. Compare the timestamps with the time the email was sent.
- A 403 within a few seconds of sending, before a person could open the message, means a scanner or prefetcher opened the link.
- A 403 about an hour after sending means the link really expired.
- Two 403s from the same address with the first one earlier means a double click or a second email after the first one worked.
Supabase's documentation explains the scanner case. Some email providers scan incoming mail for threats and open the links in it, and Supabase names Safe Links in Microsoft Defender for Office 365 as an example. The scan uses up the {{ .ConfirmationURL }} link, so the real user sees an error. This hits corporate and school addresses much more than personal Gmail addresses, which is why it works on your laptop and fails for your first business customer.
Fix 1: send a code instead of a link
A scanner can open a URL. It cannot type a code into your form. Supabase documents this as the first option. Change the email template in Authentication, Email Templates so it contains {{ .Token }}, the 6 digit code, instead of the confirmation link:
<h2>Your confirmation code</h2>
<p>Enter this code in the app to finish signing up:</p>
<p style="font-size:24px;letter-spacing:4px"><strong>{{ .Token }}</strong></p>
<p>The code works once and expires in one hour.</p>
Then add a screen where the user enters the email and the code, and verify it:
const { data, error } = await supabase.auth.verifyOtp({
email,
token: code,
type: "email",
});
if (error) {
setMessage("That code is wrong or has expired. Request a new one.");
} else {
navigate("/dashboard");
}
Use type: "email" for sign up and sign in codes, as in Supabase's example. For a password reset code, use type: "recovery", then call supabase.auth.updateUser({ password }) to set the new password.
Fix 2: keep the link, but make the user click a button
If you want a clickable link, point it at a page on your own domain and spend the token only when a person presses a button. Supabase documents a variant of this under email prefetching and gives the building blocks, the {{ .TokenHash }} variable and verifyOtp, which sends a POST request. Put this in the template:
<a href="{{ .SiteURL }}/auth/confirm?token_hash={{ .TokenHash }}&type=email">Confirm email address</a>
Then build the page at /auth/confirm. It must not verify on load. It only shows a button:
import { useSearchParams, useNavigate } from "react-router-dom";
import type { EmailOtpType } from "@supabase/supabase-js";
import { supabase } from "@/lib/supabase";
export default function ConfirmEmail() {
const [params] = useSearchParams();
const navigate = useNavigate();
const tokenHash = params.get("token_hash");
const type = params.get("type") as EmailOtpType | null;
async function confirm() {
if (!tokenHash || !type) return;
const { error } = await supabase.auth.verifyOtp({ token_hash: tokenHash, type });
if (error) navigate("/login?error=link_expired");
else navigate("/dashboard");
}
return (
<main>
<h1>Confirm your email</h1>
<button onClick={confirm}>Confirm and continue</button>
</main>
);
}
The reason for the button is simple. A scanner that opens your page with a GET request sees a page and a button. It does not click. The token is untouched until a real person acts. The same reasoning applies to server code: if a route handler calls verifyOtp on a plain GET, a scanner that visits it will use up the token just as it would have used the default link. For password reset emails, use type=recovery in the link and send the user to an update password form after verification.
If the link really expired
The default expiry is one hour. You can change it under Authentication, Sign In / Providers, Email, as Email OTP expiration. The setting also applies to magic links, password recovery, email change and invites. Supabase strongly discourages values above 86,400 seconds, and values that high can only be set through the Management API. Raising the expiry makes sense if your users read mail the next morning. It does nothing for the scanner problem.
Other causes worth ruling out
- Double click or a second email. The first click worked and the second one is rejected. Disable your button after the first press and tell users to look for a signed in tab.
- Email link tracking. If you use custom SMTP, many services rewrite links for click tracking, and Supabase's production checklist warns that this can overwrite or deform the confirmation link. Turn tracking off for auth mail.
- Wrong Site URL or redirect. If the link opens
localhostor the wrong domain, fix the URL settings first. The auth redirect to localhost guide covers that.
Show a useful message instead of a blank page
When verification fails Supabase still redirects, so your homepage may load with the error in the URL. Read it and show something helpful:
const hash = new URLSearchParams(window.location.hash.slice(1));
const query = new URLSearchParams(window.location.search);
const code = hash.get("error_code") ?? query.get("error_code");
if (code === "otp_expired") {
showBanner("That link was already used or has expired. Request a new one below.");
}
// the resend button
await supabase.auth.resend({ type: "signup", email });
Remember that resend counts against your email limits. If resend fails with a 429, read the email rate limit exceeded guide.
Prevention
Test the full sign up flow with a Microsoft 365 or school address, not only your own Gmail. Decide on the code or button pattern before launch, because you cannot fix a first impression after it has failed. Keep the Site URL and redirect list correct for production. And never test by clicking the same link twice and calling it a bug.
FAQ
Why does the link say expired when I click it right away?
Most often a security scanner in the recipient's mail system opened the link first. Supabase links work once, so the scanner used it up. Microsoft Defender Safe Links is one well known example in Supabase's own documentation.
How long is a Supabase email link valid?
One hour by default. The Email OTP expiration setting under Authentication, Sign In / Providers, Email controls it, and it also covers magic links, password recovery, email change and invite links. Values above 86,400 seconds are strongly discouraged.
Should I raise the expiry to fix this?
Only if people really take longer than an hour. If the link fails on the first click, a longer expiry changes nothing, because the token was already used by a scanner.
What is the difference between this and Token has expired or is invalid?
They are the same family of errors. The redirect shows Email link is invalid or has expired, and calls to verifyOtp or the verify endpoint return a message like Token has expired or is invalid. Both are an otp_expired situation.
Does it matter that the user opens the link on another device?
The one time token itself works on any device. But if your app uses the PKCE flow, the browser that started the login must be the one that finishes it, so test the flow on a phone as well as a laptop.
Related guides
- Supabase login redirects to localhost (or the preview URL) after deploy: how to fix it
- Supabase auth email not sending in a Lovable app: the SMTP setup that actually works
- Supabase AuthApiError: Invalid Refresh Token: Refresh Token Not Found
- Supabase signUp fails with "Database error saving new user": fix the auth.users trigger
- Supabase Edge Function returns 401 "Invalid JWT": find who rejected the call and fix it