Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

Fix GitHub OAuth login for migrated users (#2620)

Amruth Pillai18e8aadf18bb
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 (
<DropdownMenu>
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,
};
},