fix(login): preserve OIDC request context during email verification (#11990)

# Which Problems Are Solved

When a user needed to verify their email during an OIDC login flow, the
requestId was lost, preventing the flow from completing with a redirect
back to the relying party.

# How the Problems Are Solved

In `sendVerification()`, the session cookie lookup used `"loginName" in
command` to decide whether to fall back to `user.preferredLoginName`.
Since the `loginName` property key always exists in the command object
(even when its value is undefined), the fallback was never triggered.
This caused the lookup to search for loginName === undefined, finding no
session — and ultimately redirecting to a dead-end success page.

- /authenticator/set redirect: Added missing requestId to the URL params
when redirecting users who need to set up a primary auth method.
- /verify/success page: Added a "Continue" button that re-enters the
login flow with requestId preserved, handling the edge case where no
session cookie exists (e.g. email link opened in a different browser).
This commit is contained in:
Max Peintner
2026-04-08 10:25:43 +02:00
committed by GitHub
parent fbb6a406c8
commit 17f49323dd
17 changed files with 156 additions and 3 deletions
+94
View File
@@ -123,6 +123,100 @@ describe("sendVerification", () => {
// Verify creation was attempted
expect(mockCreateSessionAndUpdateCookie).toHaveBeenCalled();
});
test("should include requestId in /authenticator/set redirect when provided", async () => {
// 1. No existing session cookie
mockGetSessionCookieByLoginName.mockResolvedValue(undefined);
// 2. No auth methods (user needs setup)
mockListAuthenticationMethodTypes.mockResolvedValue({
authMethodTypes: [],
});
// 3. Mock session creation success
mockCreateSessionAndUpdateCookie.mockResolvedValue({
session: {
id: "new-session-id",
factors: { user: { id: "user-1", loginName: "[email protected]", organizationId: "org-1" } },
},
});
const result = await sendVerification({
userId: "user-1",
code: "123456",
isInvite: false,
requestId: "oidc_auth-req-123",
});
// Expect redirect to include requestId
expect(result).toEqual({
redirect: expect.stringContaining("/authenticator/set?"),
});
const redirectUrl = (result as { redirect: string }).redirect;
const params = new URLSearchParams(redirectUrl.split("?")[1]);
expect(params.get("requestId")).toBe("oidc_auth-req-123");
expect(params.get("sessionId")).toBe("new-session-id");
expect(params.get("loginName")).toBe("[email protected]");
});
test("should NOT include requestId in /authenticator/set redirect when not provided", async () => {
// 1. No existing session cookie
mockGetSessionCookieByLoginName.mockResolvedValue(undefined);
// 2. No auth methods (user needs setup)
mockListAuthenticationMethodTypes.mockResolvedValue({
authMethodTypes: [],
});
// 3. Mock session creation success
mockCreateSessionAndUpdateCookie.mockResolvedValue({
session: {
id: "new-session-id",
factors: { user: { id: "user-1", loginName: "[email protected]", organizationId: "org-1" } },
},
});
const result = await sendVerification({
userId: "user-1",
code: "123456",
isInvite: false,
// no requestId
});
const redirectUrl = (result as { redirect: string }).redirect;
const params = new URLSearchParams(redirectUrl.split("?")[1]);
expect(params.get("requestId")).toBeNull();
});
test("should fall back to user.preferredLoginName for session cookie lookup when loginName is undefined", async () => {
// Simulate the email-link scenario: loginName is undefined but userId is provided
mockGetSessionCookieByLoginName.mockResolvedValue(undefined);
mockGetUserByID.mockResolvedValue({
user: { userId: "user-1", preferredLoginName: "[email protected]" },
});
mockListAuthenticationMethodTypes.mockResolvedValue({
authMethodTypes: [],
});
mockCreateSessionAndUpdateCookie.mockResolvedValue({
session: {
id: "new-session-id",
factors: { user: { id: "user-1", loginName: "[email protected]", organizationId: "org-1" } },
},
});
await sendVerification({
userId: "user-1",
code: "123456",
isInvite: false,
loginName: undefined, // explicitly undefined, like from email link
});
// The key assertion: session cookie lookup should use preferredLoginName as fallback
expect(mockGetSessionCookieByLoginName).toHaveBeenCalledWith({
loginName: "[email protected]",
organization: undefined,
});
});
});
describe("initialSendVerification", () => {
+5 -1
View File
@@ -92,7 +92,7 @@ export async function sendVerification(command: VerifyUserByEmailCommand) {
const user = userResponse.user;
const sessionCookie = await getSessionCookieByLoginName({
loginName: "loginName" in command ? command.loginName : user.preferredLoginName,
loginName: command.loginName ?? user.preferredLoginName,
organization: command.organization,
});
@@ -156,6 +156,10 @@ export async function sendVerification(command: VerifyUserByEmailCommand) {
params.set("loginName", session.factors?.user?.loginName);
}
if (command.requestId) {
params.set("requestId", command.requestId);
}
// set hash of userId and userAgentId to prevent attacks, checks are done for users with invalid sessions and invalid userAgentId
const cookiesList = await cookies();
const userAgentId = await getOrSetFingerprintId();