redirect Uri error #1773

Closed
opened 2026-03-13 09:02:17 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @Exe16Kishan on GitHub (Aug 24, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. create express server , a client (next js) and follow installation guide according to the docs
  2. create Oauth in developer setting of Github
  3. add client id and secret to you .env and run the backend and frontend
  4. we will see the error in frontend side , like 404 page not found

Current vs. Expected behavior

according to docs it is mentioned that redirect uri default is api/auth/callback/{provider} but its not working in case of express js , i have to manually mention redirect uri to fix this

What version of Better Auth are you using?

1.3.7

System info

no idea what is this...

Which area(s) are affected? (Select all that apply)

Documentation, Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import prisma from "./db";

export const auth = betterAuth({
    database: prismaAdapter(prisma, {
        provider: "sqlite", // or "mysql", "postgresql", ...etc
    }),
    socialProviders: {
        github: {
            clientId: process.env.GITHUB_CLIENT_ID as string,
            clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
            mapProfileToUser: (profile) => {
                return {
                    name: profile.name,
                    email: profile.email,
                };
            },

            
            //redirectURI: "http://localhost:3001/api/auth/callback/github"  // uncomment this and it will work
            
        },
    
        
    }
});

Additional context

like i fixed this i want you to update this in docs so that other dont get stuck in this problem in future

Originally created by @Exe16Kishan on GitHub (Aug 24, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. create express server , a client (next js) and follow installation guide according to the docs 2. create Oauth in developer setting of Github 3. add client id and secret to you .env and run the backend and frontend 4. we will see the error in frontend side , like 404 page not found ### Current vs. Expected behavior according to docs it is mentioned that redirect uri default is api/auth/callback/{provider} but its not working in case of express js , i have to manually mention redirect uri to fix this ### What version of Better Auth are you using? 1.3.7 ### System info ```bash no idea what is this... ``` ### Which area(s) are affected? (Select all that apply) Documentation, Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import prisma from "./db"; export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "sqlite", // or "mysql", "postgresql", ...etc }), socialProviders: { github: { clientId: process.env.GITHUB_CLIENT_ID as string, clientSecret: process.env.GITHUB_CLIENT_SECRET as string, mapProfileToUser: (profile) => { return { name: profile.name, email: profile.email, }; }, //redirectURI: "http://localhost:3001/api/auth/callback/github" // uncomment this and it will work }, } }); ``` ### Additional context like i fixed this i want you to update this in docs so that other dont get stuck in this problem in future
GiteaMirror added the expressdocumentation labels 2026-03-13 09:02:17 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 24, 2025):

Hi @Exe16Kishan! I'm Dosu and I’m helping the better-auth team.

The root cause of the redirect URI error is that Better Auth's default callback route for OAuth providers is actually /oauth2/callback/{provider} (e.g., /api/auth/oauth2/callback/github), not /api/auth/callback/{provider} as some documentation suggests. If your Express backend doesn't serve this exact route, or if your baseURL isn't set correctly, you'll get a 404 after authentication. The recommended fix is to explicitly set the redirectURI in your provider config to match your backend's callback route, just as you did. This is especially important for custom setups or when your routing doesn't match the default expectations. For reference, the code and docs confirm this behavior and the need for manual configuration in Express setups (source, source).

It's a good idea for the docs to clarify this for Express users—explicitly setting redirectURI and ensuring your backend routes all /api/auth/* requests to the Better Auth handler will prevent these issues.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Aug 24, 2025): <!-- Greeting --> Hi @Exe16Kishan! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> The root cause of the redirect URI error is that Better Auth's default callback route for OAuth providers is actually `/oauth2/callback/{provider}` (e.g., `/api/auth/oauth2/callback/github`), not `/api/auth/callback/{provider}` as some documentation suggests. If your Express backend doesn't serve this exact route, or if your baseURL isn't set correctly, you'll get a 404 after authentication. The recommended fix is to explicitly set the `redirectURI` in your provider config to match your backend's callback route, just as you did. This is especially important for custom setups or when your routing doesn't match the default expectations. For reference, the code and docs confirm this behavior and the need for manual configuration in Express setups ([source](https://github.com/better-auth/better-auth/blob/8c23eb07396478a51b4dc7de669c010cdadc5479/packages/better-auth/src/plugins/generic-oauth/index.ts), [source](https://github.com/better-auth/better-auth/issues/320#issuecomment-2435458851)). It's a good idea for the docs to clarify this for Express users—explicitly setting `redirectURI` and ensuring your backend routes all `/api/auth/*` requests to the Better Auth handler will prevent these issues. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/a1c07663-bdef-43ae-a186-e75dc7d9895a?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/a1c07663-bdef-43ae-a186-e75dc7d9895a?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/a1c07663-bdef-43ae-a186-e75dc7d9895a?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/a1c07663-bdef-43ae-a186-e75dc7d9895a?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/a1c07663-bdef-43ae-a186-e75dc7d9895a?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/a1c07663-bdef-43ae-a186-e75dc7d9895a?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/a1c07663-bdef-43ae-a186-e75dc7d9895a?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/4194)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1773