mirror of
https://github.com/RayLabsHQ/gitea-mirror.git
synced 2026-07-22 03:41:52 -05:00
[PR #307] fix(sso): repair SSO login bounce + migrate to @better-auth/oauth-provider #3578
Reference in New Issue
Block a user
📋 Pull Request Information
Original PR: https://github.com/RayLabsHQ/gitea-mirror/pull/307
Author: @arunavo4
Created: 5/29/2026
Status: 🔄 Open
Base:
main← Head:fix/sso-login-and-oauth-provider-migration📝 Commits (1)
06f9c13fix(sso): repair SSO login bounce + migrate to @better-auth/oauth-provider📊 Changes
15 files changed (+544 additions, -169 deletions)
View changed files
📝
.env.example(+3 -0)📝
bun.lock(+3 -0)📝
docs/ENVIRONMENT_VARIABLES.md(+1 -0)📝
docs/SSO_TESTING.md(+43 -2)➕
drizzle/0012_oauth_provider_migration.sql(+126 -0)📝
drizzle/meta/_journal.json(+7 -0)📝
package.json(+1 -0)📝
scripts/validate-migrations.ts(+56 -0)📝
src/components/oauth/ConsentPage.tsx(+15 -27)📝
src/lib/auth-client.ts(+2 -2)📝
src/lib/auth.ts(+71 -11)📝
src/lib/db/index.ts(+4 -2)📝
src/lib/db/schema.ts(+97 -45)📝
src/pages/api/auth/oauth2/register.ts(+5 -5)📝
src/pages/api/sso/applications.ts(+110 -75)📄 Description
Fixes #306 and migrates the deprecated
oidc-providerplugin to@better-auth/oauth-provider(the deprecation warning seen in the issue logs).Part 1 — #306 SSO login bounce (the reported bug)
The reporter clicks SSO (Authentik), is bounced back to
/login. Root cause: after the OAuth round-trip the browser lands on/with no session, soMainLayoutredirects to/login. The401 /api/sso/applicationsin the report is unrelated noise (it backs the OAuth provider consent UI).Fixes:
src/lib/auth.ts): addedaccount.accountLinkingwithtrustedProviders: ["email-password"]. Better Auth enables linking by default, but auto-linking an SSO sign-in to a pre-existing email/password account only happens for trusted providers or verified emails — otherwise the sign-in fails and bounces. This is the most likely cause of #306.DEBUG(it's not thedebugnpm package), so the docs'DEBUG=better-auth:*was a no-op and the reporter saw nothing. Added aloggerblock driven byBETTER_AUTH_LOG_LEVEL(debug|info|warn|error, defaultwarn). Updateddocs/SSO_TESTING.md,docs/ENVIRONMENT_VARIABLES.md,.env.example.This half is config/docs only and directly addresses #306.
Part 2 —
oidcProvider→oauthProvidermigration (breaking, provider/IdP side)auth.ts:oidcProvider→oauthProvider; added requiredjwt()plugin (JWKS signing keys); portedgetAdditionalUserInfoClaim→customUserInfoClaims/customIdTokenClaims.auth-client.ts:oidcClient→oauthProviderClient.ConsentPage.tsx: updated to the new/oauth2/consentflow (passesoauth_query, navigates to returnedredirect_uri).schema.ts):oauth_applications→oauth_clients, reshapedoauth_access_tokens, newoauth_refresh_tokens,oauth_consent→oauth_consents, plusjwks.0012: data-preserving — copies registered clients and converts comma-separatedredirect_urls→ JSONstring[]. Access tokens/consents are dropped (ephemeral / cheaply re-granted)./api/sso/applicationsand/api/auth/oauth2/registeragainst the new plugin API (createOAuthClient/registerOAuthClient/*OAuthClient).0012upgrade fixture toscripts/validate-migrations.ts.Verification status
✅ Verified locally
0012applies cleanly on a fresh DB; old tables dropped, new tables present;redirect_urls→ JSON transform correct (asserted in the migration-validation fixture).auth.tsconstructs and exposescreateOAuthClient/registerOAuthClient/oauth2Consent.⚠️ NOT verifiable locally — needs manual testing before merge (no IdP runtime; project can't run
astro check/build locally)/.well-known/oauth-authorization-serversits under/api/auth(due tobasePath). RPs expecting it at root may need the exportedoauthProviderAuthServerMetadatahandler mounted at/. Follow-up.Test plan
BETTER_AUTH_LOG_LEVEL=debugsurfaces[Better Auth]callback trace🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.