fix: prevent double triggering of verification emails (#11995)

# Which Problems Are Solved

This fixes a critical bug where initial verification emails (or invite
codes) were occasionally sent twice, invalidating the first code and
confusing users.

Previously, the initial verification email was triggered via a
`send=true` URL parameter executing inside a frontend useEffect exactly
when the `/verify` page mounted. This was fragile and prone to race
conditions caused by component remounts or partial hydration.

# How the Problems Are Solved

- Removed `send=true` from URL state and ripped out the doSend effect in
`VerifyForm.tsx`.
- Shifted execution strictly to the Next.js server. The email is now
automatically dispatched via await `initialSendVerification(...)` during
the POST requests (acting over `sendLoginname`, `register`, `password`,
`passkeys`, and `idp`).
- The login flow is now idempotent and robust against unintended
frontend re-renders.
- Refactored `checkEmailVerification()` to be async and updated the
associated unit-test coverage (all tests passing).
This commit is contained in:
Max Peintner
2026-06-29 11:42:45 +03:00
committed by GitHub
parent 14874d6546
commit 3a18cef281
14 changed files with 359 additions and 305 deletions
+14 -2
View File
@@ -26,6 +26,7 @@ import {
} from "../zitadel";
import { createSessionAndUpdateCookie } from "./cookie";
import { getPublicHost } from "./host";
import { trySendVerification } from "./verify";
const logger = createLogger("loginname");
@@ -341,15 +342,26 @@ export async function sendLoginname(command: SendLoginnameCommand) {
// If the user's email is not verified, they likely already have a code from the
// initial verification email. Auto-sending a new one here invalidates their existing code
// and causes confusion. Only auto-send (`send=true`) if the email is already verified.
// and causes confusion. Only auto-send if the email is already verified.
const shouldSend = humanUser?.email?.isVerified === true;
const codeSent = shouldSend
? await trySendVerification({
userId: session?.factors?.user?.id ?? user.userId,
isInvite: true,
requestId: command.requestId,
})
: false;
const params = new URLSearchParams({
loginName: (session?.factors?.user?.loginName ?? user.preferredLoginName) as string,
send: shouldSend ? "true" : "false",
invite: "true", // always send invite code if user has no primary auth method
});
if (codeSent) {
params.append("codeSent", "true");
}
if (command.requestId) {
params.append("requestId", command.requestId);
}