BA
Better Auth•4d ago
Duki

Is it possible to set callbackUrl on server-side email sign Ups?

I would like to handle my sign ups on the server side, but I'm struggling to find a proper way to set my callbackUrl in the documentation for the email verification. Tried it like this, but this doesn't seem to work
await auth.api.signUpEmail({
body: {
name: username,
email,
password
},
asResponse: true,
callbackUrl: '/my-url'
});
await auth.api.signUpEmail({
body: {
name: username,
email,
password
},
asResponse: true,
callbackUrl: '/my-url'
});
How can I set it properly? Or do I have to add it manually in auth.ts?
5 Replies
Duki
DukiOP•4d ago
I also don't seem to get any autocompletion here, which makes it a lot harder to figure out which properties I can define here. Any way to fix that issue too? My tsconfigs also has the strict property set to true.
rtmorgan
rtmorgan•3d ago
I believe callbackURL is a key within body like name, email, password:
body: {
name: username,
email,
password,
callbackURL
}
body: {
name: username,
email,
password,
callbackURL
}
https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/api/routes/sign-up.ts#L49
varundeva
varundeva•3d ago
Maybe it's not working I didn't checked But I'm using redirect() in onSuccess method
Duki
DukiOP•3d ago
doesn't work either 😦 The lack of server side documentation is really frustrating 😂 I don't think i have that option on the server. Plus I want the redirect to happen after the email verification, not after the sign up. I have a feeling that this can't be done on the server side yet. I think I will just set the searchParams my self in the sendVerificationEmail callback in auth.ts. This works
sendVerificationEmail: async ({ user, url }) => {
const newUrl = new URL(url);
newUrl.searchParams.set('callbackURL', '/my-url');

await sendEmail({
to: user.email,
subject: 'Verify your email address',
html: `<a href="${newUrl.toString()}">Click here to verify your email!</a>`
});
},
sendVerificationEmail: async ({ user, url }) => {
const newUrl = new URL(url);
newUrl.searchParams.set('callbackURL', '/my-url');

await sendEmail({
to: user.email,
subject: 'Verify your email address',
html: `<a href="${newUrl.toString()}">Click here to verify your email!</a>`
});
},
I just wish there was a cleaner way to set the callback url on the server. This feels a bit brute force-y
rtmorgan
rtmorgan•2h ago
It's supported on the server-side as well with 'auth.api.sendVerificationEmail' - there is a '/send-verification-email' endpoint used by it that accepts a callbackURL for after the email verification: https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/api/routes/email-verification.ts#L78

Did you find this page helpful?