Account Linking

Hi there, I'm using better-auth for authentication in my app, and I want to support two providers: Google OAuth Phone number with OTP (credentials-based) Here's what I'm trying to achieve: Users can sign up using Google, but we want to mandate phone number verification right after sign-up to prevent fake or bot accounts. Once verified, the phone number should be linked to the Google account so that: The user can later sign in using either Google or just their phone number (one unified account). Both login methods point to the same user account. My Questions: How do I properly implement phone number + OTP signup using Better Auth? The docs aren’t super clear on this flow. After a user signs up with Google, how do I link their phone number to their Google account for dual sign-in support? Would appreciate a clearer explanation or code sample for this flow. Thanks!
1 Reply
KiNFiSH
KiNFiSH2w ago
may be over here you can do additional fields for the
user: {
additionalFields: {
// other additional feilds
phoneNumber: {
type: "string",
nullable: true,
unique: true,
},
phoneNumberVerified: {
type: "boolean",
default: false,
},
}
}
user: {
additionalFields: {
// other additional feilds
phoneNumber: {
type: "string",
nullable: true,
unique: true,
},
phoneNumberVerified: {
type: "boolean",
default: false,
},
}
}
so for this now you can make it work with otp with profile mapping using mapProfileToUser -
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
mapProfileToUser: async (profile) => {
return {
email: profile.email,
name: profile.name,
emailVerified: profile.email_verified,
image: profile.picture,
// Initialize phone fields as null
phoneNumber: null,
phoneNumberVerified: false
};
}
}
},

socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
mapProfileToUser: async (profile) => {
return {
email: profile.email,
name: profile.name,
emailVerified: profile.email_verified,
image: profile.picture,
// Initialize phone fields as null
phoneNumber: null,
phoneNumberVerified: false
};
}
}
},

after this you can use database hook to fill in the phoneNumber i mean actual phone number and handle the verification there - the hook might be route based like you can setup a hook to be triggered after signup with hook as auth middleware here and you can also go with the way of databasehook which helps to mutate the data based on your need with the life cycle. - https://www.better-auth.com/docs/concepts/hooks . this might be a good route i can think of and would love to help you on your way to implementing it
Hooks | Better Auth
Better Auth Hooks let you customize BetterAuth's behavior

Did you find this page helpful?