11import { useCallback , useEffect , useRef , useState } from "react" ;
22import {
33 exchangeHandoffCode ,
4+ ManagedAuthApiError ,
45 retrieveManagedAuth ,
56 streamManagedAuthEvents ,
67 submitFieldValues ,
@@ -55,6 +56,7 @@ function mergeStateEvent(
5556 ...base ,
5657 flow_status : ev . flow_status ,
5758 flow_step : ev . flow_step ,
59+ flow_type : ev . flow_type ?? base . flow_type ?? null ,
5860 discovered_fields : ev . discovered_fields ?? null ,
5961 pending_sso_buttons : ev . pending_sso_buttons ?? null ,
6062 mfa_options : ev . mfa_options ?? null ,
@@ -63,6 +65,9 @@ function mergeStateEvent(
6365 website_error : ev . website_error ?? null ,
6466 error_message : ev . error_message ?? null ,
6567 error_code : ev . error_code ?? null ,
68+ post_login_url : ev . post_login_url ?? base . post_login_url ?? null ,
69+ live_view_url : ev . live_view_url ?? base . live_view_url ?? null ,
70+ hosted_url : ev . hosted_url ?? base . hosted_url ?? null ,
6671 } ;
6772}
6873
@@ -79,6 +84,7 @@ export interface ManagedAuthSessionValue {
7984 state : ManagedAuthResponse | null ;
8085 uiState : UIState ;
8186 isSubmitting : boolean ;
87+ isReconnecting : boolean ;
8288 submitError : string | null ;
8389 initError : string | null ;
8490 startFlow : ( ) => void ;
@@ -101,6 +107,7 @@ export function useManagedAuthSession(
101107 const [ state , setState ] = useState < ManagedAuthResponse | null > ( null ) ;
102108 const [ uiState , setUIState ] = useState < UIState > ( "prime" ) ;
103109 const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
110+ const [ isReconnecting , setIsReconnecting ] = useState ( false ) ;
104111 const [ submitError , setSubmitError ] = useState < string | null > ( null ) ;
105112 const [ initError , setInitError ] = useState < string | null > ( null ) ;
106113
@@ -156,6 +163,7 @@ export function useManagedAuthSession(
156163
157164 const handleStateEvent = ( ev : ManagedAuthStateEventData ) => {
158165 reconnectAttemptsRef . current = 0 ;
166+ setIsReconnecting ( false ) ;
159167 setSubmitError ( null ) ;
160168 const base = stateRef . current ;
161169 if ( ! base ) return ;
@@ -186,6 +194,7 @@ export function useManagedAuthSession(
186194
187195 const scheduleReconnect = ( ) => {
188196 if ( terminalRef . current ) return ;
197+ setIsReconnecting ( true ) ;
189198 const attempt = reconnectAttemptsRef . current ++ ;
190199 const delay = Math . min (
191200 RECONNECT_BASE_MS * Math . pow ( 2 , attempt ) ,
@@ -229,7 +238,8 @@ export function useManagedAuthSession(
229238 connectStream ( t ) ;
230239 } catch ( err ) {
231240 if ( gen !== generationRef . current ) return ;
232- const status = ( err as { status ?: number } ) ?. status ;
241+ const status =
242+ err instanceof ManagedAuthApiError ? err . status : undefined ;
233243 if ( status === 401 || status === 410 ) {
234244 terminalRef . current = true ;
235245 setUIState ( "expired" ) ;
@@ -437,6 +447,7 @@ export function useManagedAuthSession(
437447 state,
438448 uiState,
439449 isSubmitting,
450+ isReconnecting,
440451 submitError,
441452 initError,
442453 startFlow,
0 commit comments