Fix GitHub OAuth login for migrated users (#2620)

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
This commit is contained in:
Amruth Pillai
2026-01-27 13:03:09 +01:00
committed by GitHub
parent 4840a292df
commit 18e8aadf18
2 changed files with 36 additions and 3 deletions
+1 -1
View File
@@ -60,7 +60,7 @@ export function UserDropdownMenu({ children }: Props) {
});
}
if (!session) return null;
if (!session?.user) return null;
return (
<DropdownMenu>
+35 -2
View File
@@ -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,
};
},