How can i get email from resetPassword

const { data, error } = await authClient.resetPassword({
newPassword: password,
token: token as string,
});
const { data, error } = await authClient.resetPassword({
newPassword: password,
token: token as string,
});
I want to send email to the user to notify that password is changed. But their is only status in data object. How can i get the email of the user? This is part of forgotPassword flow.
Solution:
Oh just use the token and find the session in your db, and based on that you get the userId, and then you can get the user from that
Jump to solution
8 Replies
preAGIcoder
preAGIcoderOP2mo ago
@Ping any idea?
Ping
Ping2mo ago
You should be calling
const { data, error } = await authClient.forgetPassword({
redirectTo: "/reset-password",
});
const { data, error } = await authClient.forgetPassword({
redirectTo: "/reset-password",
});
and on your auth instance, you can get their email to send:
import { betterAuth } from "better-auth";
import { sendEmail } from "./email"; // your email sending function

export const auth = betterAuth({
emailAndPassword: {
enabled: true,
sendResetPassword: async ({user, url, token}, request) => {
await sendEmail({
to: user.email,
subject: "Reset your password",
text: `Click the link to reset your password: ${url}`,
});
},
},
});
import { betterAuth } from "better-auth";
import { sendEmail } from "./email"; // your email sending function

export const auth = betterAuth({
emailAndPassword: {
enabled: true,
sendResetPassword: async ({user, url, token}, request) => {
await sendEmail({
to: user.email,
subject: "Reset your password",
text: `Click the link to reset your password: ${url}`,
});
},
},
});
preAGIcoder
preAGIcoderOP2mo ago
I am already doing that and reset passoword is working. My question is about sending an email after the password the changed to notify the user.
Ping
Ping2mo ago
Oh I see. An idea is to just send a request to a backend endpoint, and using the headers you'll know the user id. From there, query your db to find the user's email, then send the email. Make sure to pass your users session in to headers ^ And also take into consideration of rate-limits and security
preAGIcoder
preAGIcoderOP2mo ago
But user is not loged in yet right? since the password reset is based on token. I am not sure how can i pass the session to headers, since i dont have the session yet, after reset passoword is successful lmk if i am missing something
Solution
Ping
Ping2mo ago
Oh just use the token and find the session in your db, and based on that you get the userId, and then you can get the user from that
preAGIcoder
preAGIcoderOP2mo ago
Thanks, i got the user id from verification table, it was in value column
Ping
Ping2mo ago
Oh, okay cool

Did you find this page helpful?