@@ -317,6 +317,9 @@ const SignIn: FC<SignInProps> = ({className, size = 'medium', onSuccess, onError
317317 return false ;
318318 } ;
319319
320+ /**
321+ * Initialize the flow and handle cleanup of stale flow state.
322+ */
320323 useEffect ( ( ) => {
321324 const storedFlowId = sessionStorage . getItem ( 'asgardeo_flow_id' ) ;
322325 const urlParams = getUrlParams ( ) ;
@@ -329,52 +332,36 @@ const SignIn: FC<SignInProps> = ({className, size = 'medium', onSuccess, onError
329332
330333 handleSessionDataKey ( urlParams . sessionDataKey ) ;
331334
332- if ( urlParams . code ) {
333- const flowIdFromState = resolveFlowId (
334- currentFlowId ,
335- urlParams . state ,
336- urlParams . flowId ,
337- storedFlowId ,
338- ) ;
339-
340- // Only process code if we have a valid flowId to use
341- if ( flowIdFromState ) {
342- setFlowId ( flowIdFromState ) ;
343- setIsFlowInitialized ( true ) ;
344- initializationAttemptedRef . current = true ;
345- // Clean up flowId from URL after setting it in state
346- cleanupFlowUrlParams ( ) ;
347- } else {
348- console . warn ( '[SignIn] OAuth code in URL but no valid flowId found. Cleaning up stale OAuth parameters.' ) ;
349- cleanupOAuthUrlParams ( true ) ;
350- }
335+ // Skip OAuth code processing - let the dedicated OAuth useEffect handle it
336+ if ( urlParams . code || urlParams . state ) {
351337 return ;
352338 }
353339
354- // If flowId is in URL or sessionStorage but no code and no active flow state
355- if ( ( urlParams . flowId || storedFlowId ) && ! urlParams . code && ! currentFlowId ) {
340+ // If flowId is in URL or sessionStorage but no active flow state, clean it up
341+ // This handles stale flowIds from previous sessions or incomplete flows
342+ if ( ( urlParams . flowId || storedFlowId ) && ! currentFlowId ) {
356343 console . warn (
357- '[SignIn] FlowId in URL/sessionStorage but no active flow state detected. '
344+ '[SignIn] FlowId in URL/sessionStorage but no active flow state detected. Cleaning up stale flowId. '
358345 ) ;
359346 setFlowId ( null ) ;
360347 sessionStorage . removeItem ( 'asgardeo_flow_id' ) ;
361348 cleanupFlowUrlParams ( ) ;
362349 // Continue to initialize with applicationId instead
363350 }
364351
352+ // Only initialize if we're not processing an OAuth callback or submission
353+ const currentUrlParams = getUrlParams ( ) ;
365354 if (
366355 isInitialized &&
367356 ! isLoading &&
368357 ! isFlowInitialized &&
369358 ! initializationAttemptedRef . current &&
370- ! currentFlowId
359+ ! currentFlowId &&
360+ ! currentUrlParams . code &&
361+ ! currentUrlParams . state &&
362+ ! isSubmitting &&
363+ ! oauthCodeProcessedRef . current
371364 ) {
372- // Clean up any stale OAuth parameters before starting a new flow
373- const urlParams = getUrlParams ( ) ;
374- if ( urlParams . code || urlParams . state ) {
375- console . debug ( '[SignIn] Cleaning up stale OAuth parameters before starting new flow' ) ;
376- cleanupOAuthUrlParams ( true ) ;
377- }
378365 initializationAttemptedRef . current = true ;
379366 initializeFlow ( ) ;
380367 }
@@ -535,6 +522,8 @@ const SignIn: FC<SignInProps> = ({className, size = 'medium', onSuccess, onError
535522 if ( flowId && components ) {
536523 setFlowId ( flowId ) ;
537524 setComponents ( components ) ;
525+ // Ensure flow is marked as initialized when we have components
526+ setIsFlowInitialized ( true ) ;
538527 // Clean up flowId from URL after setting it in state
539528 cleanupFlowUrlParams ( ) ;
540529 }
@@ -598,7 +587,6 @@ const SignIn: FC<SignInProps> = ({className, size = 'medium', onSuccess, onError
598587
599588 if ( ! currentFlowId ) {
600589 setFlowId ( flowIdToUse ) ;
601- setIsFlowInitialized ( true ) ;
602590 }
603591 const submitPayload : EmbeddedSignInFlowRequestV2 = {
604592 flowId : flowIdToUse ,
0 commit comments