From 8b5320d6ccb0dcb05cd87635eaa20c5cd0ff8ad7 Mon Sep 17 00:00:00 2001 From: yuhengshs Date: Fri, 22 Nov 2024 13:44:17 -0800 Subject: [PATCH] fix: set active username after auth attempt to maintain consistent user context --- .../src/client/flows/userAuth/handleUserAuthFlow.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/auth/src/client/flows/userAuth/handleUserAuthFlow.ts b/packages/auth/src/client/flows/userAuth/handleUserAuthFlow.ts index 24ba20500a6..753ac66db04 100644 --- a/packages/auth/src/client/flows/userAuth/handleUserAuthFlow.ts +++ b/packages/auth/src/client/flows/userAuth/handleUserAuthFlow.ts @@ -18,6 +18,7 @@ import { getAuthUserAgentValue } from '../../../utils'; import { handlePasswordSRP } from '../shared/handlePasswordSRP'; import { assertValidationError } from '../../../errors/utils/assertValidationError'; import { AuthValidationErrorCode } from '../../../errors/types/validation'; +import { setActiveSignInUsername } from '../../../providers/cognito/utils/signInHelpers'; export interface HandleUserAuthFlowInput { username: string; @@ -107,11 +108,18 @@ export async function handleUserAuthFlow({ }), }); - return initiateAuth( + const response = await initiateAuth( { region: getRegionFromUserPoolId(userPoolId), userAgentValue: getAuthUserAgentValue(AuthAction.SignIn), }, jsonReq, ); + + // Set the active username immediately after successful authentication attempt + // If a user starts a new sign-in while another sign-in is incomplete, + // this ensures we're tracking the correct user for subsequent auth challenges. + setActiveSignInUsername(username); + + return response; }