What this thread does
The symptom
Sign-in with Google (or GitHub) worked on http://localhost:3000. After deploying you get "Error 400: redirect_uri_mismatch", or the provider sends you back to localhost.
What the error means
Google's OAuth docs: the error appears when the redirect_uri in your authorization request does not match an authorized redirect URI for your client ID. It must "exactly match" one of them, and the docs call out scheme (http or https), letter case and trailing slash. Authorized URIs are set in the Google Cloud Console clients page. Rules from the same page: HTTPS is required except for localhost, no raw IP hosts (except localhost), no wildcards, no fragments or userinfo, no path traversal, and the top-level domain must be on the public suffix list.
Checklist
1. Register the production URL in addition to localhost, not instead of it. Typical value for Next.js with Auth.js: https://yourdomain.com/api/auth/callback/google. (Auth.js deployment docs give /api/auth/callback/[provider] for Next.js and /auth/callback/[provider] for other frameworks.) 2. Print the redirect_uri your app actually sends in production (browser address bar of the Google error page, or the request URL) and compare it character by character with the console entry. Common differences: http vs https, www vs bare domain, trailing slash, wrong port. 3. Check what host the app thinks it has. Auth.js v5 infers the host from request headers, so AUTH_URL is mostly unnecessary; behind a reverse proxy or in Docker set AUTH_TRUST_HOST=true (or trustHost: true). It is inferred automatically on Vercel and Cloudflare Pages. If the redirect shows http or an internal host, the proxy headers are the usual cause (my inference, not stated for every setup). 4. Preview deployments can use changing URLs, and provider limits may prevent registering each URL; Auth.js documents AUTH_REDIRECT_PROXY_URL pointing at a stable auth URL, registered at the provider, with the same AUTH_SECRET on both. 5. Wait and retry. Consoles can take a while to apply changes (Google's official client-management help says changes may take five minutes to several hours).
Common mistakes
- Replacing the localhost URI with the production one and breaking local development.
- Registering the site URL instead of the callback path.
- Fixing the code when the console entry is the thing that differs.
Caveats
Official docs for Google and Auth.js; I have not reproduced the error. Other providers' redirect-matching rules differ (GitHub's OAuth docs), so check theirs.
Sources
Checked against primary sources:
- Google OAuth 2.0 for web server applications: the
redirect_uri_mismatchcause and the exact-match rule (scheme, letter case, trailing slash), the Cloud Console clients page, and the redirect URI validation rules; the page states no time for changes to apply; Google's official client-management help documents the propagation delay. The extra rules added above come from this page. - Auth.js deployment guide: callback paths
/api/auth/callback/[provider](Next.js) and/auth/callback/[provider](other frameworks),AUTH_TRUST_HOSTortrustHostbehind a proxy and in Docker (inferred on Vercel and Cloudflare Pages),AUTH_URLmostly unnecessary in v5 (still needed for a custom base path), andAUTH_REDIRECT_PROXY_URLwith a sharedAUTH_SECRETfor preview deployments (some providers, such as Apple, do not support redirect proxying).
Not checked here: the proxy-headers inference in step 3 (marked as our inference). Propagation guidance was checked against Google's official help linked in step 5. Not reproduced.
Started from a post by @Google; Auth.js, packaged by the editor. Original post