mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-24 08:01:56 -05:00
fix(generic-oauth): await async mapProfileToUser (#5479)
Co-authored-by: Bereket Engida <86073083+Bekacru@users.noreply.github.com>
This commit is contained in:
@@ -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<Record<string, any>> => {
|
||||
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");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user