Fix guide · updated 2026-11-16
Supabase "Token has expired or is invalid" (email OTP)
{"code":"otp_expired","message":"Token has expired or is invalid"}
Your app emails a code, the user types it, and verifyOtp() fails with AuthApiError: Token has expired or is invalid, a 403 with the code otp_expired. Usually the code hasn't expired at all. Supabase gives this same answer whenever the code it receives isn't the one it has on file for that email and that type. We ran Supabase Auth itself (version 2.198, November 2026) and reproduced each of the seven causes below. Here's how to find yours.
What the error means
When your app calls supabase.auth.verifyOtp({ email, token, type }), Supabase finds the user by the email address, then compares the code with the one it stored when it sent the email. It answers 403 with {"code":"otp_expired","message":"Token has expired or is invalid"} when:
- no user has that email address,
- the code isn't the one stored for that
type, - the code was already used: each code works once,
- the code is older than the expiry time.
A failed attempt doesn't use up the code. In our test, the right code still worked after three wrong attempts, so once you fix the cause, the user can try the same code again.
The one-minute check
Log exactly what your app sends, right before the call:
console.log(JSON.stringify({ email, token, type }))
const { data, error } = await supabase.auth.verifyOtp({ email, token, type })
Compare it with the newest email: the same address, the same digits (as many as in the email, no spaces, and in quotes, so it's text and not a number), and a type of 'email', or 'recovery' for a password reset. Remove the log when you're done.
1. The wrong type
signInWithOtp() sends one of two emails: Confirm signup to an address Supabase doesn't know yet, Magic Link to an existing user. Supabase keeps the two codes in separate places, and type tells it where to look:
'email'looks in both: use it for every code fromsignInWithOtp(), and for sign-up confirmation codes,'signup'only finds a new user's code, and'magiclink'only an existing user's,'recovery'is for codes fromresetPasswordForEmail().
In our test, a new user's code failed with 'magiclink' and 'recovery' and worked with 'email'. An existing user's code failed with 'signup' and worked with 'email'. So with the wrong type, codes fail only for new users, or only for returning ones.
const { data, error } = await supabase.auth.verifyOtp({ email, token, type: 'email' })
For a password reset, keep type: 'recovery'. 'email' also accepted a reset code in our test, but supabase-js then reports a normal sign-in (SIGNED_IN) instead of PASSWORD_RECOVERY, and an app that waits for that event never shows its new-password form.
2. A newer code replaced it
Each new email replaces the code, and only the newest one works. Supabase refuses a second request for the same address within 60 seconds (a 429, "For security purposes, you can only request this after 59 seconds"), and then the first code stays valid. After that minute, Resend sends a new code and the first one stops working. In our test, with the wait shortened to 3 seconds, the first email's code failed and the second one's worked.
A sign-in code and a password-reset code for the same user are kept in the same place: in our test, a sign-in request sent after a reset email killed the reset code.
- Under the code field, write "Use the code from the newest email".
- Send a code only when the user presses the button. A code screen that sends one each time it opens kills the previous code whenever it opens again more than a minute later.
- Grey out Resend for 60 seconds, and empty the code field when it sends.
3. The link in the same email was opened first
If the email has a link and a code, both use the same token. Opening the link signs in with it, and the code then fails. In our test, one visit to the link, which is what an email security scanner does, and the typed code got "Token has expired or is invalid". The user can do it too, by tapping the link on their phone and then typing the code on their laptop. Supabase's troubleshooting page calls the scanner version email prefetching, and names it the most common cause of this error.
Fix: in Authentication → Email Templates, keep only {{ .Token }} in the templates your app uses (Magic Link and Confirm signup) and remove {{ .ConfirmationURL }}. A scanner can read a code, but it can't use it.
4. The digits aren't the ones in the email
Supabase compares the exact text it receives, and supabase-js sends the code exactly as you pass it. Three ways the code changes on the way:
- 8 digits in a 6-digit field. The code length is a project setting, Email OTP length, from 6 to 10 digits, next to the expiry setting in cause 7. Supabase's docs describe 6-digit codes, but count the digits in your own email. In our test with 8-digit codes, the first 6 digits failed and all 8 worked.
- A space. A code pasted as " 811234" failed in our test.
- A lost leading zero. If your code turns the input into a number (
Number(),parseInt, a number field in a form library), 044020 becomes 44020. In our test, 44020 failed and "044020" worked.
Keep the code as text, keep only its digits, and let the field take the length your project sends:
// code = the text from the field, never Number(code) or parseInt(code)
const token = code.replace(/\D/g, '') // digits only
const { data, error } = await supabase.auth.verifyOtp({ email, token, type: 'email' })
5. The code was already used
Each code works once. In our test, the same code sent a second time got "Token has expired or is invalid". This happens when your app calls verifyOtp() twice: a field that submits by itself on the last digit plus a button that submits again, or a double tap. The first call signs the user in, the second one fails, and the app shows the error to a user who is in fact signed in. Ignore new submissions while a call is running, and stop after the first success.
6. Another email address
Supabase finds the user by the address you pass. In our test, the right code with another address got the same 403. Capitals don't matter, because Supabase lowercases the address. Pass the exact address the code was sent to, kept from the first screen in your app's state or in the URL, rather than one the user types again.
If the email is missing entirely, for example because the state was lost when the code screen opened, Supabase answers something else: 400 "Only an email address or phone number should be provided on verify". Without a type, it's 400 "Verify requires a verification type".
7. The code is too old
Codes expire after 1 hour by default. The setting is Email OTP expiration, in seconds, under Authentication → Sign In / Providers → Auth Providers → Email. In our test, with the expiry set to 6 seconds, a code typed 7 seconds after it was sent failed. The same setting covers every email link, and the dashboard allows at most 86,400 seconds (one day). Rather than raising it, make sure users can ask for a new code.
If an AI tool built your app
Give your AI tool this prompt:
Find every supabase.auth.verifyOtp call. Use type: 'email' for codes from signInWithOtp and type: 'recovery' for codes from resetPasswordForEmail. Pass the same email string that was used to send the code. Keep the code as a string (never Number or parseInt), remove everything but digits, and let the code field accept up to 10 digits. Ignore new submissions while a verifyOtp call is running. Send a code only when the user presses the send button, never when a screen opens, and disable Resend for 60 seconds after each send. Don't change anything else.
The email template (cause 3) and the code length are dashboard settings, so those parts are yours.
Not quite your error?
- The user clicks the link in the email and lands on your site with
otp_expired: Supabase "Email link is invalid or has expired". - "For security purposes, you can only request this after 59 seconds", or "email rate limit exceeded": Supabase "email rate limit exceeded" error.
- The code never arrives: Supabase: "Error sending confirmation email".
- "Signups not allowed for otp": Supabase "Signups not allowed for this instance".
Still stuck?
Open the logs in your Supabase dashboard and find the failed request to /verify (status 403). Then look at what happened just before for the same user: an earlier request to /verify means the link was opened first (cause 3), and a request to /otp or /recover after the email the user read means a newer code (cause 2). Supabase's page on OTP verification failures describes the same check.
Other Supabase problems? See Supabase fixes.