Can I use Phone Number plugin to just add phone number to existing users?

Basically I just wanna add phone number to existing users. Can I use Phone number plugin to do so or should I implement it myself? because Everytime I verify the phone number. callbackOnVerification return null for the user. Here is my code: client:
await authClient.phoneNumber.verify({
phoneNumber,
code,
disableSession: true,
fetchOptions: {
onError: (ctx) => {
form.setError("code", {
message: `phone-number.OTP.${ctx.error.code}`,
});
},
onSuccess: () => {
setIsDialogOpen(false);
setDialogState("InputNumber");
},
},
});
});
await authClient.phoneNumber.verify({
phoneNumber,
code,
disableSession: true,
fetchOptions: {
onError: (ctx) => {
form.setError("code", {
message: `phone-number.OTP.${ctx.error.code}`,
});
},
onSuccess: () => {
setIsDialogOpen(false);
setDialogState("InputNumber");
},
},
});
});
/lib/auth.ts:
export const auth = betterAuth({
database: prismaAdapter(db, {
provider: "postgresql",
}),
plugins: [
phoneNumber({
sendOTP: ({ phoneNumber, code }, request) => {
// TODO: Implement sending OTP code via Whatsapp
console.log({
phoneNumber,
code,
});
},
callbackOnVerification: async ({ phoneNumber, user }, request) => {
console.log({
user,
});
// await db.user.update({
// where: {
// id: user?.id,
// },
// data: {
// phoneNumber,
// phoneNumberVerified: true,
// },
// });
},
}),
],
});
export const auth = betterAuth({
database: prismaAdapter(db, {
provider: "postgresql",
}),
plugins: [
phoneNumber({
sendOTP: ({ phoneNumber, code }, request) => {
// TODO: Implement sending OTP code via Whatsapp
console.log({
phoneNumber,
code,
});
},
callbackOnVerification: async ({ phoneNumber, user }, request) => {
console.log({
user,
});
// await db.user.update({
// where: {
// id: user?.id,
// },
// data: {
// phoneNumber,
// phoneNumberVerified: true,
// },
// });
},
}),
],
});
6 Replies
basic white guy
basic white guy2mo ago
did you figure this out? having the same issue User can come back as null, the user I want to update wont come back event tho they exist
bekacru
bekacru2mo ago
you need to send them otp and verify their otp to get it updated. But you can also just use updateUser to update the user phone number @basic white guy
basic white guy
basic white guy2mo ago
I running the send a verify in the same file
await authClient.phoneNumber.sendOtp(
{
phoneNumber: data.phoneNumber,
},
{
onError: (error) => {
toast({
title: "OTP Send Failed",
description: error.error.message,
variant: "destructive",
});
},
onSuccess: () => {
setPage(2);
},
},
);
await authClient.phoneNumber.sendOtp(
{
phoneNumber: data.phoneNumber,
},
{
onError: (error) => {
toast({
title: "OTP Send Failed",
description: error.error.message,
variant: "destructive",
});
},
onSuccess: () => {
setPage(2);
},
},
);
await authClient.phoneNumber.verify(
{
phoneNumber: form.getValues("phoneNumber"),
code: pin,
// disableSession: true,
},
{
onError: (error) => {
toast({
title: "OTP Verification Failed",
description: error.error.message,
variant: "destructive",
});
},
onSuccess: (ctx) => {
console.log("🚀 ~ Success handleVerifyOTP ~ ctx:", ctx);
},
},
);
await authClient.phoneNumber.verify(
{
phoneNumber: form.getValues("phoneNumber"),
code: pin,
// disableSession: true,
},
{
onError: (error) => {
toast({
title: "OTP Verification Failed",
description: error.error.message,
variant: "destructive",
});
},
onSuccess: (ctx) => {
console.log("🚀 ~ Success handleVerifyOTP ~ ctx:", ctx);
},
},
);
this is triggering callbackOnVerification but its not updating the user table would you recommend in the onSuccess of the .verify to update the user table, since callbackOnVerification doesnt return anything?
bekacru
bekacru2mo ago
it shouldn't require callbackOnVerification you can pass updatePhoneNumber:true to update the phone number
await authClient.phoneNumber.verify(
{
phoneNumber: form.getValues("phoneNumber"),
code: pin,
updatePhoneNumber: true // here
},
{
onError: (error) => {
toast({
title: "OTP Verification Failed",
description: error.error.message,
variant: "destructive",
});
},
onSuccess: (ctx) => {
console.log("🚀 ~ Success handleVerifyOTP ~ ctx:", ctx);
},
},
);
await authClient.phoneNumber.verify(
{
phoneNumber: form.getValues("phoneNumber"),
code: pin,
updatePhoneNumber: true // here
},
{
onError: (error) => {
toast({
title: "OTP Verification Failed",
description: error.error.message,
variant: "destructive",
});
},
onSuccess: (ctx) => {
console.log("🚀 ~ Success handleVerifyOTP ~ ctx:", ctx);
},
},
);
basic white guy
basic white guy2mo ago
thank you its updating it
Maqed
MaqedOP2mo ago
Thank you! It worked! ❤️

Did you find this page helpful?