diff --git a/packages/better-auth/src/plugins/generic-oauth/generic-oauth.test.ts b/packages/better-auth/src/plugins/generic-oauth/generic-oauth.test.ts index da810edef5..c87e6bc4ea 100644 --- a/packages/better-auth/src/plugins/generic-oauth/generic-oauth.test.ts +++ b/packages/better-auth/src/plugins/generic-oauth/generic-oauth.test.ts @@ -961,4 +961,42 @@ describe("oauth2", async () => { expect(session.data?.user.email).toBe("oauth2-cookie-state@test.com"); expect(session.data?.user.name).toBe("OAuth2 Cookie State"); }); + + it("should await async mapProfileToUser", async () => { + const { auth } = await getTestInstance({ + plugins: [ + genericOAuth({ + config: [ + { + providerId: "test-async", + clientId: clientId, + clientSecret: clientSecret, + getUserInfo: async (_tokens) => ({ + id: "test-user-id", + email: "test@example.com", + name: "Test User", + emailVerified: true, + }), + mapProfileToUser: async ( + _profile, + ): Promise> => { + return { customField: "async-custom-data" }; + }, + }, + ], + }), + ], + }); + + const context = await auth.$context; + const provider = context.socialProviders.find((p) => p.id === "test-async"); + + const result = await provider!.getUserInfo({ + accessToken: "test-access-token", + idToken: undefined, + refreshToken: undefined, + }); + + expect(result?.user).toHaveProperty("customField", "async-custom-data"); + }); }); diff --git a/packages/better-auth/src/plugins/generic-oauth/index.ts b/packages/better-auth/src/plugins/generic-oauth/index.ts index e02f2ba83a..37894595b8 100644 --- a/packages/better-auth/src/plugins/generic-oauth/index.ts +++ b/packages/better-auth/src/plugins/generic-oauth/index.ts @@ -321,6 +321,9 @@ export const genericOAuth = (options: GenericOAuthOptions) => { if (!userInfo) { return null; } + + const userMap = await c.mapProfileToUser?.(userInfo); + return { user: { id: userInfo?.id, @@ -328,7 +331,7 @@ export const genericOAuth = (options: GenericOAuthOptions) => { emailVerified: userInfo?.emailVerified, image: userInfo?.image, name: userInfo?.name, - ...c.mapProfileToUser?.(userInfo), + ...userMap, }, data: userInfo, };