From 18e8aadf18bb5eaa126592350458b1f4fb81f7b0 Mon Sep 17 00:00:00 2001 From: Amruth Pillai Date: Tue, 27 Jan 2026 13:03:09 +0100 Subject: [PATCH] Fix GitHub OAuth login for migrated users (#2620) Co-authored-by: Cursor Agent --- src/components/user/dropdown-menu.tsx | 2 +- src/integrations/auth/config.ts | 37 +++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/components/user/dropdown-menu.tsx b/src/components/user/dropdown-menu.tsx index 48bc1e16e..af7bded9a 100644 --- a/src/components/user/dropdown-menu.tsx +++ b/src/components/user/dropdown-menu.tsx @@ -60,7 +60,7 @@ export function UserDropdownMenu({ children }: Props) { }); } - if (!session) return null; + if (!session?.user) return null; return ( diff --git a/src/integrations/auth/config.ts b/src/integrations/auth/config.ts index ecf0ee420..1055469c3 100644 --- a/src/integrations/auth/config.ts +++ b/src/integrations/auth/config.ts @@ -1,4 +1,5 @@ import { BetterAuthError } from "@better-auth/core/error"; +import { and, eq, or } from "drizzle-orm"; import { passkey } from "@better-auth/passkey"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import { betterAuth } from "better-auth/minimal"; @@ -161,12 +162,44 @@ const getAuthConfig = () => { // biome-ignore lint/style/noNonNullAssertion: enabled check ensures these are not null clientSecret: env.GITHUB_CLIENT_SECRET!, mapProfileToUser: async (profile) => { + const login = profile.login ?? String(profile.id); + const normalizedLogin = toUsername(login); + const [legacyAccount] = await db + .select({ + accountId: schema.account.accountId, + email: schema.user.email, + emailVerified: schema.user.emailVerified, + username: schema.user.username, + displayUsername: schema.user.displayUsername, + }) + .from(schema.account) + .innerJoin(schema.user, eq(schema.account.userId, schema.user.id)) + .where( + and( + eq(schema.account.providerId, "github"), + or(eq(schema.user.username, normalizedLogin), eq(schema.user.displayUsername, login)), + ), + ) + .limit(1); + + if (legacyAccount) { + return { + id: legacyAccount.accountId, + name: profile.name, + email: legacyAccount.email, + image: profile.avatar_url, + username: legacyAccount.username, + displayUsername: legacyAccount.displayUsername, + emailVerified: legacyAccount.emailVerified, + }; + } + return { name: profile.name, email: profile.email, image: profile.avatar_url, - username: profile.login, - displayUsername: profile.login, + username: normalizedLogin, + displayUsername: login, emailVerified: true, }; },