Rejected authorization code

Sign in as you+badcode@example.com and the provider rejects the authorization code when your server exchanges it for tokens. Login and consent work as usual, so the failure happens in your callback route, the same place it happens in production. Use it to check that your app shows a retry instead of a 500.

Use your own Mockatoo address with +badcode and your usual password. You can combine it with other tags, e.g. you+badcode.unverified@example.com; see Email tags.

What each provider returns

Every provider rejects the code with its own error:

ProviderWhat your app gets
GoogleThe authorization code exchange returns invalid_grant.
MicrosoftThe authorization code exchange returns invalid_grant.
GitHubThe authorization code exchange returns bad_verification_code.
AppleThe authorization code exchange returns invalid_grant.
DiscordThe authorization code exchange returns invalid_grant.
LinkedInThe authorization code exchange returns invalid_request ("Unable to retrieve access token: …").
XThe authorization code exchange returns invalid_request ("Value passed for the authorization code was invalid.").
FacebookThe authorization code exchange returns OAuthException code 100, "Invalid verification code format."

When it happens in production

An authorization code can be used once, and only for a short time. The provider rejects the exchange when the code was already used, for example because the user reloaded the callback page or the browser sent the request twice. It also rejects a code that expired, or one sent with a different redirect_uri or PKCE verifier than the authorization request.

Test it with Playwright

This test signs in with Google as you+badcode@example.com and checks that the app shows a retry message. It reads your test account from the MOCKATOO_TEST_EMAIL and MOCKATOO_TEST_PASSWORD environment variables. Change the button name and the message to match your app.

import { expect, test } from "@playwright/test";const email = process.env.MOCKATOO_TEST_EMAIL ?? "";const password = process.env.MOCKATOO_TEST_PASSWORD ?? "";test("+badcode: tells the user to retry when sign-in cannot finish", async ({	page,}) => {	const address = email.replace("@", `+e2e-${crypto.randomUUID()}.badcode@`);	// Better Auth reports a rejected authorization code as invalid_code.	// The app shows a retry message and leaves the user signed out.	await page.goto("/");	await page.getByRole("button", { name: "Sign in with Google" }).click();	await page.getByLabel("Email", { exact: true }).fill(address);	await page.getByLabel("Password", { exact: true }).fill(password);	await page.getByRole("button", { name: "Log in with password" }).click();	await page.getByRole("button", { name: "Authorize" }).click();	await expect(page).toHaveURL(		(url) =>			url.pathname === "/" && url.searchParams.get("error") === "invalid_code",	);	await expect(		page.getByRole("alert").filter({			hasText: "Sign-in failed before it finished. Try again in a minute.",		}),	).toHaveText("Sign-in failed before it finished. Try again in a minute.");	await expect(		page.getByRole("button", { name: "Sign in with Google" }),	).toBeVisible();	await expect(page.getByText("You are logged in")).toBeHidden();});

It runs in CI as tests/tags/badcode.spec.ts in the Better Auth + Next.js example.