@@ -122,6 +122,7 @@ type KindeProviderProps = {
122122 * ⚠️ Must be memoized or defined outside component to prevent effect re-runs.
123123 */
124124 activityTimeout ?: ActivityTimeoutConfig ;
125+ refreshOnFocus ?: boolean
125126} ;
126127
127128const defaultCallbacks : KindeCallbacks = {
@@ -158,6 +159,7 @@ export const KindeProvider = ({
158159 popupOptions = { } ,
159160 store = storeState . memoryStorage ,
160161 activityTimeout,
162+ refreshOnFocus = false ,
161163} : KindeProviderProps ) => {
162164 const mergedCallbacks = { ...defaultCallbacks , ...callbacks } ;
163165
@@ -605,32 +607,35 @@ export const KindeProvider = ({
605607 ) ;
606608
607609 const handleFocus = useCallback ( ( ) => {
608- if ( document . visibilityState === "visible" && state . isAuthenticated ) {
610+ if ( document . visibilityState === "visible" && state . isAuthenticated && refreshOnFocus ) {
609611 refreshToken ( { domain, clientId, onRefresh } ) . catch ( ( error ) => {
610612 console . error ( "Error refreshing token:" , error ) ;
611613 } ) ;
612614 }
613- } , [ state . isAuthenticated , domain , clientId , onRefresh ] ) ;
615+ } , [ state . isAuthenticated , domain , clientId , onRefresh , refreshOnFocus ] ) ;
614616
615617 useEffect ( ( ) => {
616618 // remove any existing event listener before adding a new one
619+
617620 document . removeEventListener ( "visibilitychange" , handleFocus ) ;
621+ if ( refreshOnFocus ) {
618622 document . addEventListener ( "visibilitychange" , handleFocus ) ;
619- return ( ) => {
620- document . removeEventListener ( "visibilitychange" , handleFocus ) ;
621- } ;
622- } , [ handleFocus ] ) ;
623+ return ( ) => {
624+ document . removeEventListener ( "visibilitychange" , handleFocus ) ;
625+ } ;
626+ }
627+ } , [ handleFocus , refreshOnFocus ] ) ;
623628
624629 const init = useCallback ( async ( ) => {
625630 if ( initRef . current ) return ;
626631 try {
627632 try {
633+ initRef . current = true ;
628634 await checkAuth ( { domain, clientId } ) ;
629635 } catch ( err ) {
630636 console . warn ( "checkAuth failed:" , err ) ;
631637 setState ( ( v : ProviderState ) => ( { ...v , isLoading : false } ) ) ;
632638 }
633- initRef . current = true ;
634639 const params = new URLSearchParams ( window . location . search ) ;
635640
636641 if ( params . has ( "error" ) ) {
0 commit comments