[PR #4799] [MERGED] feat(better-auth): sendOnSignIn check for email sent with username sign in Closes: #4808 #22483

Closed
opened 2026-04-15 21:04:45 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4799
Author: @QuintenStr
Created: 9/21/2025
Status: Merged
Merged: 9/22/2025
Merged by: @Bekacru

Base: canaryHead: feat-added-check-for-sendOnSignIn-on-username-sign-in


📝 Commits (1)

  • 859ace1 feat: sendOnSignIn check for email sent with username sign in

📊 Changes

1 file changed (+31 additions, -4 deletions)

View changed files

📝 packages/better-auth/src/plugins/username/index.ts (+31 -4)

📄 Description

Clearly describe what changes you made and why

I have added a check for the "sendOnSignIn" variable when authenticating yourself with usernames. When you sign in with email, this was already present. However, by username (since its a plugin) this wasn't implemented yet, and therefore emails could be spammed when signing in with username.

Include any relevant context or background

/packages/better-auth/src/plugins/username/index.ts:
Previous behaviour:

if (
	!user.emailVerified &&
	ctx.context.options.emailAndPassword?.requireEmailVerification
) {
	await sendVerificationEmailFn(ctx, user);
	throw new APIError("FORBIDDEN", {
		message: ERROR_CODES.EMAIL_NOT_VERIFIED,
	});
}

New behaviour: (copied the flow from email sign-in)

if (
	ctx.context.options?.emailAndPassword?.requireEmailVerification &&
	!user.emailVerified
	) {
		if (
			!ctx.context.options?.emailVerification?.sendVerificationEmail
		) {
			throw new APIError("FORBIDDEN", {
			message: ERROR_CODES.EMAIL_NOT_VERIFIED,
		});
	}
	
	if (ctx.context.options?.emailVerification?.sendOnSignIn) {
		const token = await createEmailVerificationToken(
			ctx.context.secret,
			user.email,
			undefined,
			ctx.context.options.emailVerification?.expiresIn,
		);
		const url = `${ctx.context.baseURL}/verify-email?token=${token}&callbackURL=${
			ctx.body.callbackURL || "/"
		}`;
		await ctx.context.options.emailVerification.sendVerificationEmail(
			{
				user: user,
				url,
				token,
			},
			ctx.request,
		);
	}
	throw new APIError("FORBIDDEN", {
		message: ERROR_CODES.EMAIL_NOT_VERIFIED,
	});
}

List any breaking changes or deprecations

No breaking changes found. All tests still run.

Add screenshots for UI changes

No UI changes.

Not applicable.


Summary by cubic

Adds sendOnSignIn handling to the username sign-in flow to match email sign-in and prevent unwanted verification emails.

  • Bug Fixes
    • Respect emailVerification.sendOnSignIn and sendVerificationEmail when emailAndPassword.requireEmailVerification is true and the user’s email is unverified.
    • Create and send a verification token only when sendOnSignIn is enabled; otherwise return FORBIDDEN with EMAIL_NOT_VERIFIED.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/4799 **Author:** [@QuintenStr](https://github.com/QuintenStr) **Created:** 9/21/2025 **Status:** ✅ Merged **Merged:** 9/22/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `feat-added-check-for-sendOnSignIn-on-username-sign-in` --- ### 📝 Commits (1) - [`859ace1`](https://github.com/better-auth/better-auth/commit/859ace1846a7349cede0a02880b930dc5dabf95d) feat: sendOnSignIn check for email sent with username sign in ### 📊 Changes **1 file changed** (+31 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/username/index.ts` (+31 -4) </details> ### 📄 Description ### Clearly describe what changes you made and why I have added a check for the "sendOnSignIn" variable when authenticating yourself with usernames. When you sign in with email, this was already present. However, by username (since its a plugin) this wasn't implemented yet, and therefore emails could be spammed when signing in with username. ### Include any relevant context or background /packages/better-auth/src/plugins/username/index.ts: Previous behaviour: ``` if ( !user.emailVerified && ctx.context.options.emailAndPassword?.requireEmailVerification ) { await sendVerificationEmailFn(ctx, user); throw new APIError("FORBIDDEN", { message: ERROR_CODES.EMAIL_NOT_VERIFIED, }); } ``` New behaviour: _(copied the flow from email sign-in)_ ``` if ( ctx.context.options?.emailAndPassword?.requireEmailVerification && !user.emailVerified ) { if ( !ctx.context.options?.emailVerification?.sendVerificationEmail ) { throw new APIError("FORBIDDEN", { message: ERROR_CODES.EMAIL_NOT_VERIFIED, }); } if (ctx.context.options?.emailVerification?.sendOnSignIn) { const token = await createEmailVerificationToken( ctx.context.secret, user.email, undefined, ctx.context.options.emailVerification?.expiresIn, ); const url = `${ctx.context.baseURL}/verify-email?token=${token}&callbackURL=${ ctx.body.callbackURL || "/" }`; await ctx.context.options.emailVerification.sendVerificationEmail( { user: user, url, token, }, ctx.request, ); } throw new APIError("FORBIDDEN", { message: ERROR_CODES.EMAIL_NOT_VERIFIED, }); } ``` ### List any breaking changes or deprecations No breaking changes found. All tests still run. ### Add screenshots for UI changes No UI changes. ### Reference related issues or discussions Not applicable. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds sendOnSignIn handling to the username sign-in flow to match email sign-in and prevent unwanted verification emails. - **Bug Fixes** - Respect emailVerification.sendOnSignIn and sendVerificationEmail when emailAndPassword.requireEmailVerification is true and the user’s email is unverified. - Create and send a verification token only when sendOnSignIn is enabled; otherwise return FORBIDDEN with EMAIL_NOT_VERIFIED. <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 21:04:45 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#22483