Demo, all content is generated
Question

GitHub login: 'The redirect_uri is not associated with this application' on localhost only

Solved · 641 views · asked by dirk_vl · edited

Auth.js (next-auth v5) with GitHub provider. Production works. Locally I get the GitHub error page:

The redirect_uri is not associated with this application.
The application might be misconfigured or could be trying to redirect you somewhere you might not expect.

The GitHub OAuth app has callback URL https://mytool.app/api/auth/callback/github. There's only one field for it.

What I’ve tried

Tried putting both URLs comma separated in the field, GitHub didn't accept that. Set AUTH_URL to localhost in .env.local.

Comment
Is that a GitHub OAuth App or a GitHub App? They behave differently here. rafa_dev · edited
OAuth App dirk_vl · edited

3 answers

Marked as helpful by the asker
rafa_dev · edited

A GitHub OAuth App allows exactly one callback URL, and the redirect must match its host and port. localhost:3000 isn't mytool.app, so it's rejected.

Standard setup: create a second OAuth App just for development, callback http://localhost:3000/api/auth/callback/github, and use its client id/secret in .env.local:

AUTH_GITHUB_ID=dev_app_id
AUTH_GITHUB_SECRET=dev_app_secret

Production keeps its own pair in Vercel env vars. Bonus: you can never accidentally break prod login while experimenting.

Comment
Two apps, of course. Working now, thanks. dirk_vl · edited
chidi_eze · edited

FYI, a GitHub App (not OAuth App) supports several callback URLs. Only worth switching if you also need the other GitHub App features; for login alone, two OAuth Apps is simpler.

Comment
kofi_mensah · edited

Same issue will hit you on Vercel previews, since every preview has its own URL. If you need login on previews, give previews a stable branch domain (e.g. staging.mytool.app) and a third OAuth app for that.

Comment
Yes, next thing I would have run into. Thanks. dirk_vl · edited