Generic OAuth custom callback url #336

Closed
opened 2026-03-13 07:42:12 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @danielbwh on GitHub (Dec 4, 2024).

Is your feature request related to a problem? Please describe.
I am attempting to swap out next-auth for better-auth, and we use Okta at work. Since there is no built-in provider for better-auth and okta at this time, I am using the generic oauth plugin.

Describe the solution you'd like
I would like to set a custom callback url, one that does not require oauth2 be in the URL. In our next-auth implementation the callback url is '/api/auth/callback/okta', but in better-auth it has to be changed to '/api/auth/oauth2/callback/okta'. Since we are in a bigger company, changing the URL in the Okta app requires a change request with another internal team. I was hoping to avoid that to save time while swapping in better-auth.

Describe alternatives you've considered
I did try redirecting the URL's in the middleware, like this:

// Redirect old callback URL to new callback URL
  if (request.nextUrl.pathname === '/api/auth/callback/okta') {
    const newUrl = new URL('/api/auth/oauth2/callback/okta', request.url);
    newUrl.search = request.nextUrl.search; // Forward search parameters
    return NextResponse.redirect(newUrl);
  }

And it gets close, but comes back with in invalid client error:

 ✓ Compiled /api/auth/[...all] in 3.1s (2082 modules)
 GET /api/auth/get-session?currentURL=http%3A%2F%2Flocalhost%3A3000%2F 200 in 3737ms
 POST /api/auth/sign-in/oauth2?currentURL=http%3A%2F%2Flocalhost%3A3000%2F 200 in 951ms
 GET /api/auth/get-session?currentURL=http%3A%2F%2Flocalhost%3A3000%2F 200 in 6ms
 ✓ Compiled /middleware in 176ms (73 modules)

 ERROR    { error: 'invalid_client',                                                                                                                                    Better Auth
  error_description:
   'Client authentication failed. Either the client or the client credentials are invalid.',
  status: 401,
  statusText: 'Unauthorized' }

Additional context
There may be something I am doing wrong here and what I want is possible? Thank you for your time, very excited about this auth package!

Originally created by @danielbwh on GitHub (Dec 4, 2024). **Is your feature request related to a problem? Please describe.** I am attempting to swap out next-auth for better-auth, and we use Okta at work. Since there is no built-in provider for better-auth and okta at this time, I am using the generic oauth plugin. **Describe the solution you'd like** I would like to set a custom callback url, one that does not require oauth2 be in the URL. In our next-auth implementation the callback url is `'/api/auth/callback/okta'`, but in better-auth it has to be changed to `'/api/auth/oauth2/callback/okta'`. Since we are in a bigger company, changing the URL in the Okta app requires a change request with another internal team. I was hoping to avoid that to save time while swapping in better-auth. **Describe alternatives you've considered** I did try redirecting the URL's in the middleware, like this: ``` // Redirect old callback URL to new callback URL if (request.nextUrl.pathname === '/api/auth/callback/okta') { const newUrl = new URL('/api/auth/oauth2/callback/okta', request.url); newUrl.search = request.nextUrl.search; // Forward search parameters return NextResponse.redirect(newUrl); } ``` And it gets close, but comes back with in invalid client error: ``` ✓ Compiled /api/auth/[...all] in 3.1s (2082 modules) GET /api/auth/get-session?currentURL=http%3A%2F%2Flocalhost%3A3000%2F 200 in 3737ms POST /api/auth/sign-in/oauth2?currentURL=http%3A%2F%2Flocalhost%3A3000%2F 200 in 951ms GET /api/auth/get-session?currentURL=http%3A%2F%2Flocalhost%3A3000%2F 200 in 6ms ✓ Compiled /middleware in 176ms (73 modules) ERROR { error: 'invalid_client', Better Auth error_description: 'Client authentication failed. Either the client or the client credentials are invalid.', status: 401, statusText: 'Unauthorized' } ``` **Additional context** There may be something I am doing wrong here and what I want is possible? Thank you for your time, very excited about this auth package!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#336