Sign in with Google without redirect_uri_mismatch
Set up Sign in with Google: create the OAuth web client, list exact redirect URIs per environment, fix redirect_uri_mismatch, and publish the app.
One client, one exact address
Sign in with Google needs an OAuth client of type Web application, created by hand in the Google Cloud Console, with your callback address listed character for character under Authorized redirect URIs. You get a client ID and a client secret, put both in server-side environment variables, and request only the scopes openid, email and profile. With those three scopes there is no review to wait for.
Google gives no API for creating this client on someone's behalf, so it is a step every founder does personally. The rest of this page is mostly about getting the callback address right the first time.
Redirect URIs for each environment
The redirect URI is where Google sends the visitor back after they pick an account. Your app sends it with the sign-in request, and Google compares it with the list on the client. The comparison is exact: scheme, upper and lower case, and a trailing slash all count, and wildcards are not allowed. Add one line per place the app runs.
The path depends on your auth code. The table uses /api/auth/google/callback. Auth libraries each have their own convention, so copy the path from the library's documentation or from the failing request, never from memory.
| Environment | Authorized redirect URI | Remark |
|---|---|---|
| Production | https://myapp.com/api/auth/google/callback | Must be HTTPS |
| Production on www | https://www.myapp.com/api/auth/google/callback | Only if the app really serves sign-in on www. It is a different string |
| Local development | http://localhost:3000/api/auth/google/callback | Plain HTTP is permitted for localhost only |
| Preview deployments | One line per preview hostname | No wildcard exists, so most teams test sign-in on one fixed staging address |
Seven steps in Google Auth Platform
Google has moved these screens out of APIs & Services into an area called Google Auth Platform, with pages named Branding, Audience and Clients. Older tutorials call the first two the OAuth consent screen.
- Create a Google Cloud project for this product alone and switch to it. The consent screen belongs to the project, so sharing one project between products shows the wrong name to users.
- Open Google Auth Platform and press Get started. Enter the app name and a user support email, choose the audience External, and give a contact email.
- Go to Clients and press Create client.
- Application type: Web application. Name it something you will recognise, such as Web.
- Under Authorized redirect URIs, add the lines from the table above. Leave Authorized JavaScript origins empty: it is for apps that call Google from browser JavaScript, not for a server-side flow.
- Press Create and copy the client ID and client secret immediately. Google shows the secret only at this moment; afterwards it is not visible again.
- Store them on your host as
GOOGLE_CLIENT_IDandGOOGLE_CLIENT_SECRETand redeploy.
redirect_uri_mismatch and the errors next to it
Google's error page names the problem in a short code. The first one below accounts for most failed first attempts. When you see it, read the redirect_uri parameter in the address bar of the failing request and paste precisely that string into the client. Then wait: Google notes that changes to a client can take from five minutes to a few hours to take effect, so an immediate retry that still fails is not proof the fix was wrong.
| Error | Cause | Fix |
|---|---|---|
redirect_uri_mismatch | The address your app sent is not on the client's list | Add the exact string. Usual culprits: http where https is listed, www, a trailing slash, or an app behind a proxy that believes it is served over HTTP and builds the wrong URL |
invalid_client | The client secret is wrong | Check the variable for a truncated or stale value. If the secret is lost, add a new one on the client |
deleted_client | The client was deleted, by hand or automatically for inactivity | Restore it from the deleted credentials page within 30 days |
org_internal | The audience is Internal and the visitor is outside your Google Cloud organization | Switch the audience to External |
Testing, In production, and who can sign in
A new External app starts with the publishing status Testing. The general rule for Testing is strict: at most 100 listed test users, a warning screen, and authorizations that expire after seven days.
Many guides stop there and conclude that nobody but you can sign in until you publish. Google's audience documentation makes an explicit exception for exactly the case of a sign-in button. If the app requests only name, email address and profile, through openid, email and profile, users do not have to be on the test list, see no warning, and their authorization does not expire after a week.
Publish anyway before launch, with the Publish app button on the Audience page. Testing is meant for development, and the moment you add any further scope the strict rule applies to every new user.
The app name on the consent screen needs verification
Here is the part that surprises people after publishing. For an External app in production, Google displays your app name and logo to all users only once the branding has been verified. This brand verification is lighter than the review for sensitive scopes. Google describes it as automated and usually done within minutes of pressing Verify branding, with a manual review of two to three business days when the automatic check cannot decide.
It has prerequisites you can prepare now. The home page, privacy policy and terms links must be on domains listed under Authorized domains, and so must the hosts in your redirect URIs. Ownership of those domains must be verified in Google Search Console by a Google account that is an Owner or Editor of the Cloud project. In practice: verify the domain in Search Console first, with the same Google account, and brand verification has nothing left to object to. Any later change to name, logo or links is saved as a draft and has to be verified and published again.
What to request and what to store
Store sub as the user's key, not the email address. Google states that sub is unique among all Google Accounts and never reused, while one account can have different email addresses at different times. An app keyed on email creates a second, empty account for a returning customer on the day they change their address.
Also send a random state value with each sign-in request and compare it on the callback. Google's guide lists it as the protection against request forgery, and most auth libraries do it for you.
| Scope | What it returns | Why you need it |
|---|---|---|
openid | An ID token containing sub, the account's permanent identifier | Proves who signed in |
email | The address and the email_verified flag | For contact and for matching an existing account |
profile | Name and picture | Display only. Leave it out if you show neither |
Housekeeping that bites months later
- A client that has not been used for six months is deleted automatically. Google emails a warning 30 days ahead, and a deleted client can be restored for 30 days. A staging client nobody signs in to is the typical victim.
- Secrets can be rotated without downtime: add a second secret to the client, deploy it, then disable the old one.
- The client ID is public and appears in every sign-in URL. Only the secret needs protecting, and it belongs in server variables, never in a
NEXT_PUBLIC_one.
Where Shipyard stops
Shipyard does not create the Google client, because Google offers no way for a tool to do so. What it does, when your stack uses the built-in sign-in, is list this task after a launch with your own values filled in: the project name to use, and the exact redirect URI https://yourdomain/api/auth/google/callback. It also gives you a sentence to hand your coding agent so the callback route is built at that same path.
Adding GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET to your host is your step as well; Shipyard does not collect or store these two values. Publishing the app and brand verification stay with you too.
Sources
- Using OAuth 2.0 for web server applicationsdevelopers.google.com
- Manage OAuth clients (Google Cloud Help)support.google.com
- Manage app audience (Google Cloud Help)support.google.com
- Brand verificationdevelopers.google.com
- OpenID Connectdevelopers.google.com
Launch in an hour, not a weekend
Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.
See how it works