fix(login): invite flow instead of email verification for users with no primary method, improve sending behaviour (#11837)

Closes #10929

# Which Problems Are Solved

When a human user without a primary authentication method enters their
email on the login screen, the login historically auto-sent an email
code (send=true) and redirected them to the `/verify` flow. If the user
was newly created via the API and already received an initial
verify-email, navigating to the login page would trigger a new invite
code, silently invalidating the code they received in their first email.
Additionally instead of resending the same type of email (invitation) a
regular email verification mail was sent

# How the Problems Are Solved
- Resend an invitation email instead of a email verification if a user
has no method set (still in invitation state)
- Conditional Code Sending: Updated `loginname.ts` to check
`humanUser?.email?.isVerified`. We only auto-send a new code
(`send=true`) if the user's email is already verified. Unverified users
will be redirected with `send=false`, allowing them to safely enter the
code they already have.
- UI State Fix: Fixed an issue in `verify/page.tsx` where the send URL
parameter was being checked directly as a string ("false" is truthy). By
using the properly evaluated doSend boolean, the "Code Sent" alert now
correctly hides itself when a new code is not explicitly sent.
- Translation Updates: Refined the codeSent messaging across all locales
to specify "A new code has been sent..." to provide better context to
the user when they do explicitly request a resend.
This commit is contained in:
Max Peintner
2026-04-02 09:58:20 +02:00
committed by GitHub
parent 140f07e60b
commit 891c7473ca
15 changed files with 46 additions and 17 deletions
+24 -1
View File
@@ -198,12 +198,35 @@ describe("sendLoginname", () => {
mockCreateSessionAndUpdateCookie.mockResolvedValue({ session: mockSession, sessionCookie: {} });
});
test("should redirect to verify when user has no authentication methods", async () => {
test("should redirect to verify with send=false when user has no authentication methods and email is unverified", async () => {
mockListAuthenticationMethodTypes.mockResolvedValue({ authMethodTypes: [] });
const result = await sendLoginname({
loginName: "[email protected]",
requestId: "req123",
organization: "org123",
});
expect(result).toHaveProperty("redirect");
expect((result as any).redirect).toMatch(/^\/verify\?/);
expect((result as any).redirect).toContain("loginName=user%40example.com");
expect((result as any).redirect).toContain("send=false");
expect((result as any).redirect).toContain("invite=true");
expect((result as any).redirect).toContain("requestId=req123");
});
test("should redirect to verify with send=true when user has no authentication methods and email is already verified", async () => {
const verifiedEmailUser = {
...mockUser,
type: { case: "human", value: { email: { email: "[email protected]", isVerified: true } } },
};
mockSearchUsers.mockResolvedValue({ result: [verifiedEmailUser] });
mockListAuthenticationMethodTypes.mockResolvedValue({ authMethodTypes: [] });
const result = await sendLoginname({
loginName: "[email protected]",
requestId: "req123",
organization: "org123",
});
expect(result).toHaveProperty("redirect");
+8 -2
View File
@@ -338,10 +338,16 @@ export async function sendLoginname(command: SendLoginnameCommand) {
logger.debug("humanUser.email?.isVerified", {
isVerified: humanUser?.email?.isVerified,
});
// 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.
const shouldSend = humanUser?.email?.isVerified === true;
const params = new URLSearchParams({
loginName: (session?.factors?.user?.loginName ?? user.preferredLoginName) as string,
send: "true", // set this to true to request a new code immediately
invite: humanUser?.email?.isVerified ? "false" : "true", // sendInviteEmailCode results in an error if user is already initialized
send: shouldSend ? "true" : "false",
invite: "true", // always send invite code if user has no primary auth method
});
if (command.requestId) {