Supabase “Email link is invalid or has expired” and confirmation links that open localhost

By Abheesht Tripathi · Published

#error=access_denied&error_code=otp_expired&error_description=Email+link+is+invalid+or+has+expired

What it means

The link in a Supabase sign-up, magic-link, invite or password-reset email contains a one-time token. Supabase Auth checks it when the link is opened. If the token was already used or is past its expiry, Auth redirects back with error_code=otp_expired and the message “Email link is invalid or has expired”. A related symptom is a link that works but lands on http://localhost:3000 in production. Both usually come from the same few settings.

Cause 1: the redirect URL is not on the allow list

When you pass emailRedirectTo (or redirectTo), Supabase Auth only uses it if it matches Authentication → URL Configuration → Redirect URLs. If it doesn't, Auth does not return an error. It quietly falls back to the Site URL. That is why links land on localhost: the Site URL is still the development default.

await supabase.auth.signUp({
  email,
  password,
  options: { emailRedirectTo: 'https://app.example.com/welcome' },   // must match the allow list
});

If you edited the email templates, they must use {{ .RedirectTo }} where the link should honour emailRedirectTo; a hard-coded {{ .SiteURL }} ignores it.

Cause 2: the link was already used

Each token works once. Common ways it gets used before the user clicks:

Cause 3: the link really expired

The lifetime is Email OTP Expiration under the email provider settings in Authentication. Raising it a little helps users who confirm the next morning; it does not help with scanners.

Cause 4: PKCE and a different browser

With the PKCE flow (the default in @supabase/ssr), the code verifier is stored in the browser that started sign-up. A user who signs up on a laptop and taps the link on a phone has no verifier there, so the code exchange fails.

The robust fix: token_hash and your own confirm route

Point the template at your own page and verify the token there. It works in any browser, and you can put a button on the page so a scanner's automatic visit doesn't use the token:

<!-- Authentication → Email Templates → Confirm signup -->
<a href="{{ .SiteURL }}/auth/confirm?token_hash={{ .TokenHash }}&type=email&next=/welcome">
  Confirm your email
</a>
// app/auth/confirm/route.ts (Next.js App Router)
import { type EmailOtpType } from '@supabase/supabase-js';
import { type NextRequest } from 'next/server';
import { redirect } from 'next/navigation';
import { createClient } from '@/utils/supabase/server';

export async function GET(request: NextRequest) {
  const { searchParams } = new URL(request.url);
  const token_hash = searchParams.get('token_hash');
  const type = searchParams.get('type') as EmailOtpType | null;
  const nextParam = searchParams.get('next') ?? '/';
  // only same-site paths, so the link can't become an open redirect
  const next = nextParam.startsWith('/') && !nextParam.startsWith('//') ? nextParam : '/';

  if (token_hash && type) {
    const supabase = await createClient();
    const { error } = await supabase.auth.verifyOtp({ type, token_hash });
    if (!error) redirect(next);
  }
  redirect('/auth/error');
}

To beat link scanners, make /auth/confirm a page with a “Confirm” button that calls verifyOtp, instead of verifying on the GET request itself.

How to verify

Sign up with a fresh address in production. The email link points at your domain, not localhost. Opening it once signs you in and lands on /welcome; opening it again shows your error page. Repeat with the link opened in a different browser: it still works with the token_hash route.

FAQ

Why does my Supabase confirmation link go to localhost:3000?

Because the Site URL in Authentication settings is still localhost, and either no redirect URL was given or the one you passed is not on the Redirect URLs allow list. Supabase then falls back to the Site URL without reporting an error.

Does Supabase say when a redirectTo URL is not allowed?

No. A redirect URL that doesn't match the allow list is ignored and the Site URL is used instead, so check the allow list whenever users land on the wrong page.

Related guides

More in Auth, JWT and secrets · all guides