[GH-ISSUE #3088] sendVerificationEmail error during the email change process #9470

Closed
opened 2026-04-13 04:56:48 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @TonybynMp4 on GitHub (Jun 19, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3088

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Have better auth setup to use email & password, with sendVerificationEmail & sendChangeEmailVerification.

  • Trigger authClient.changeEmail
  • Click on the link in the mail sent to the original email
    The verification email sent to the new email shouldn't arrive because the sendVerificationEmail function crashes

Current vs. Expected behavior

 POST /api/auth/change-email 200 in 843ms
# SERVER_ERROR:  TypeError: Cannot read properties of null (reading 'email')
    at Object.sendVerificationEmail (src\lib\auth.ts:100:13)

(line 100): to: user.email,

Expected: user data should be present in the function

What version of Better Auth are you using?

1.2.9

Provide environment information

- OS: Windows 11
- Browser: Edge (don't judge)
- Node: v23.6.1
- Framework: Next.js (latest)

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

export const auth = betterAuth({
	user: {
		...options.user,
		changeEmail: {
			enabled: true,
			sendChangeEmailVerification: [...]
	},
	database: drizzleAdapter(db, {
		provider: "mysql",
	}),
	emailVerification: {
		sendOnSignUp: true,
		autoSignInAfterVerification: true,
		sendVerificationEmail: async ({ user, url, token }, _) => {
			console.log(user) // => null
		},
	},
	emailAndPassword: {
		enabled: true,
		requireEmailVerification: false,
		sendResetPassword
	},
})

Additional context

No response

Originally created by @TonybynMp4 on GitHub (Jun 19, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3088 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Have better auth setup to use email & password, with sendVerificationEmail & sendChangeEmailVerification. - Trigger authClient.changeEmail - Click on the link in the mail sent to the original email The verification email sent to the new email shouldn't arrive because the sendVerificationEmail function crashes ### Current vs. Expected behavior ```sendVerificationEmail crashes because user is undefined: POST /api/auth/change-email 200 in 843ms # SERVER_ERROR: TypeError: Cannot read properties of null (reading 'email') at Object.sendVerificationEmail (src\lib\auth.ts:100:13) ``` (line 100): `to: user.email,` Expected: user data should be present in the function ### What version of Better Auth are you using? 1.2.9 ### Provide environment information ```bash - OS: Windows 11 - Browser: Edge (don't judge) - Node: v23.6.1 - Framework: Next.js (latest) ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript export const auth = betterAuth({ user: { ...options.user, changeEmail: { enabled: true, sendChangeEmailVerification: [...] }, database: drizzleAdapter(db, { provider: "mysql", }), emailVerification: { sendOnSignUp: true, autoSignInAfterVerification: true, sendVerificationEmail: async ({ user, url, token }, _) => { console.log(user) // => null }, }, emailAndPassword: { enabled: true, requireEmailVerification: false, sendResetPassword }, }) ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 04:56:48 -05:00
Author
Owner

@Kinfe123 commented on GitHub (Jun 24, 2025):

how are you calling it on the client ?

<!-- gh-comment-id:2999793665 --> @Kinfe123 commented on GitHub (Jun 24, 2025): how are you calling it on the client ?
Author
Owner

@TonybynMp4 commented on GitHub (Jun 29, 2025):

how are you calling it on the client ?

// formData: { newEmail: string }
async function onSubmit(formData: NewUserEmailFormSchema) {
	const { data, error } = await authClient.changeEmail({
		newEmail: formData.newEmail,
		callbackURL: `${window.location.origin}/dashboard`,
	})

        [...]
}
<!-- gh-comment-id:3016878901 --> @TonybynMp4 commented on GitHub (Jun 29, 2025): > how are you calling it on the client ? ```ts // formData: { newEmail: string } async function onSubmit(formData: NewUserEmailFormSchema) { const { data, error } = await authClient.changeEmail({ newEmail: formData.newEmail, callbackURL: `${window.location.origin}/dashboard`, }) [...] } ```
Author
Owner

@dosubot[bot] commented on GitHub (Sep 28, 2025):

Hi, @TonybynMp4. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported a crash in Better Auth v1.2.9's sendVerificationEmail function due to a null user object when changing email.
  • The crash happens after calling authClient.changeEmail and clicking the verification link, where user data is unexpectedly undefined.
  • You provided a code snippet showing the changeEmail call with a callbackURL as requested by the maintainer.
  • The main challenge is ensuring user data is available in sendVerificationEmail to prevent the crash.
  • The issue remains unresolved with no further updates.

Next Steps:

  • Please confirm if this issue is still relevant with the latest version of better-auth and provide any new information if available.
  • If I don’t hear back within 7 days, I will automatically close this issue.

Thanks for your understanding and contribution!

<!-- gh-comment-id:3343830999 --> @dosubot[bot] commented on GitHub (Sep 28, 2025): Hi, @TonybynMp4. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You reported a crash in Better Auth v1.2.9's sendVerificationEmail function due to a null user object when changing email. - The crash happens after calling authClient.changeEmail and clicking the verification link, where user data is unexpectedly undefined. - You provided a code snippet showing the changeEmail call with a callbackURL as requested by the maintainer. - The main challenge is ensuring user data is available in sendVerificationEmail to prevent the crash. - The issue remains unresolved with no further updates. **Next Steps:** - Please confirm if this issue is still relevant with the latest version of better-auth and provide any new information if available. - If I don’t hear back within 7 days, I will automatically close this issue. Thanks for your understanding and contribution!
Author
Owner

@TonybynMp4 commented on GitHub (Sep 28, 2025):

Can confirm it still happens (screenshot below from v1.3.18)!
It could be linked to custom session or something maybe? i'm open to share any details the team might need!

Image
<!-- gh-comment-id:3343928910 --> @TonybynMp4 commented on GitHub (Sep 28, 2025): Can confirm it still happens (screenshot below from v1.3.18)! It could be linked to custom session or something maybe? i'm open to share any details the team might need! <img width="1583" height="821" alt="Image" src="https://github.com/user-attachments/assets/51075b2b-2934-4c9e-ad2d-80c856456593" />
Author
Owner

@QuintenStr commented on GitHub (Sep 29, 2025):

Can confirm it still happens (screenshot below from v1.3.18)! It could be linked to custom session or something maybe? i'm open to share any details the team might need!

Image

Hello, I would like to look in to this. Can you provide me a repository with your exact configuration so I can replicate? Thank you!

<!-- gh-comment-id:3348995816 --> @QuintenStr commented on GitHub (Sep 29, 2025): > Can confirm it still happens (screenshot below from v1.3.18)! It could be linked to custom session or something maybe? i'm open to share any details the team might need! > > <img alt="Image" width="1583" height="821" src="https://private-user-images.githubusercontent.com/97451137/494890055-51075b2b-2934-4c9e-ad2d-80c856456593.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NTkxNzg5NzgsIm5iZiI6MTc1OTE3ODY3OCwicGF0aCI6Ii85NzQ1MTEzNy80OTQ4OTAwNTUtNTEwNzViMmItMjkzNC00YzllLWFkMmQtODBjODU2NDU2NTkzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTA5MjklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwOTI5VDIwNDQzOFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQ3ZjlmZjNlOTlkNDMwODNkZjRjZWM2OTRiNTcxMzBjNWExZjI0MDg1NzY3YjViNjA2YjNhYTdjMDVhNjFkOTEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.Z54jnUnlyRV6MxtrU_82Wsi8vH8pnhjqz67J-VPVslg"> Hello, I would like to look in to this. Can you provide me a repository with your exact configuration so I can replicate? Thank you!
Author
Owner

@TonybynMp4 commented on GitHub (Sep 30, 2025):

Hello, I would like to look in to this. Can you provide me a repository with your exact configuration so I can replicate? Thank you!

Hey, i've made a quick test repo (urls are on the terminal for simplicity, it's not my exact config but i've confirmed the bug still happens there so should be fine)
https://github.com/TonybynMp4/test-better-auth/tree/main

It ONLY happens when your email IS verified (since when it's unverified it skips the changeEmail and sends a verification email directly i think?)

I had to create an account -> changeEmail -> verify (no bug since the email isn't verified by default)

Image

email_verified is now false

then when i tried again changeEmail -> verify -> sendVerificationEmail failed (this issue's bug)
Image

<!-- gh-comment-id:3351990594 --> @TonybynMp4 commented on GitHub (Sep 30, 2025): > Hello, I would like to look in to this. Can you provide me a repository with your exact configuration so I can replicate? Thank you! Hey, i've made a quick test repo (urls are on the terminal for simplicity, it's not my exact config but i've confirmed the bug still happens there so should be fine) https://github.com/TonybynMp4/test-better-auth/tree/main It *ONLY* happens when your email *IS* verified (since when it's unverified it skips the changeEmail and sends a verification email directly i think?) I had to `create an account -> changeEmail -> verify` (no bug since the email isn't verified by default) <img width="448" height="88" alt="Image" src="https://github.com/user-attachments/assets/0df82508-c693-4f5d-a4dc-8a5ba398d924" /> `email_verified` is now `false` then when i tried again `changeEmail -> verify` -> sendVerificationEmail failed (this issue's bug) <img width="691" height="250" alt="Image" src="https://github.com/user-attachments/assets/cb0beb85-5ba5-4763-a598-9e93f5485c52" />
Author
Owner

@QuintenStr commented on GitHub (Sep 30, 2025):

Hello, quick update: I was able to pin point where the user is returning null from in the internal code. Will be looking for a fix. Keep you updated

(Line 342 in /src/api/routes/email-verification.ts returns null) -> mysql drizzle adapter is failing here to use the new values in the WHERE.

<!-- gh-comment-id:3353785772 --> @QuintenStr commented on GitHub (Sep 30, 2025): Hello, quick update: I was able to pin point where the user is returning null from in the internal code. Will be looking for a fix. Keep you updated (Line 342 in /src/api/routes/email-verification.ts returns null) -> mysql drizzle adapter is failing here to use the new values in the WHERE.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9470