BA
Better Auth•2mo ago
Doppio

mapProfileToUser doesn't work when using ID token authentication

Hi, great work on Better Auth! When using ID tokens to log in into a sign in provider (e.g. Google with ID token and access token, as aspecified here ), mapProfileToUser parameters are not passed to the user object, thus not being stored in the database neither. I've set up additionalAttributes on the user object. If those are required, Prisma complains about those parameters not being specified while creating the user object. If those are not required they are just left as null Backend auth.ts
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "mysql"
}),
plugins: [
openAPI()
],
user: {
additionalFields: {
firstName: { type: 'string', required: true },
lastName: { type: 'string', required: true },
}
},
socialProviders: {
google: {
clientId: "...",
clientSecret: "...",

mapProfileToUser: (profile) => {
return {
firstName: profile.given_name,
lastName: profile.family_name,
};
},
},
},
})
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "mysql"
}),
plugins: [
openAPI()
],
user: {
additionalFields: {
firstName: { type: 'string', required: true },
lastName: { type: 'string', required: true },
}
},
socialProviders: {
google: {
clientId: "...",
clientSecret: "...",

mapProfileToUser: (profile) => {
return {
firstName: profile.given_name,
lastName: profile.family_name,
};
},
},
},
})
Frontend code:
const data = await authClient.signIn.social({
provider: "google",
idToken: {
token: "...",
accessToken: "..."
}
})
const data = await authClient.signIn.social({
provider: "google",
idToken: {
token: "...",
accessToken: "..."
}
})
Using Better Auth v.1.2.1. Any ideas? Thank you!
1 Reply
Doppio
DoppioOP•2mo ago
@bekacru i've digged into this a bit and it seems like this PR should fix it: https://github.com/better-auth/better-auth/pull/1649 I've tried it locally and this seems to work @bekacru hi! Do you think the PR can be merged in one of the next few releases? At the moment it is a blocker for us in shipping our auth with better-auth. No rush, just for info 😄

Did you find this page helpful?