@@ -196,6 +196,23 @@ function AuthOptionsInner({
196196 const [ isRegistration , setIsRegistration ] = useState ( false ) ;
197197 const [ isSocialAuthLoading , setIsSocialAuthLoading ] = useState ( false ) ;
198198 const windowPopup = useRef < Window | null > ( null ) ;
199+ const popupCheckIntervalRef = useRef < ReturnType < typeof setInterval > | null > (
200+ null ,
201+ ) ;
202+ const authFlowCompletedRef = useRef ( false ) ;
203+
204+ const clearPopupCheck = ( ) => {
205+ if ( popupCheckIntervalRef . current ) {
206+ clearInterval ( popupCheckIntervalRef . current ) ;
207+ popupCheckIntervalRef . current = null ;
208+ }
209+ } ;
210+
211+ useEffect ( ( ) => {
212+ return ( ) => {
213+ clearPopupCheck ( ) ;
214+ } ;
215+ } , [ ] ) ;
199216
200217 const checkForOnboardedUser = async ( data : LoggedUser ) => {
201218 onAuthStateUpdate ( { isLoading : true } ) ;
@@ -383,6 +400,21 @@ function AuthOptionsInner({
383400 } ;
384401
385402 const handleLoginMessage = async ( e ?: MessageEvent ) => {
403+ authFlowCompletedRef . current = true ;
404+ clearPopupCheck ( ) ;
405+ const popup = windowPopup . current ;
406+ windowPopup . current = null ;
407+ if ( popup && ! popup . closed ) {
408+ popup . close ( ) ;
409+ // Retry after a short delay — some browsers defer the close when the
410+ // popup is still settling after a redirect chain.
411+ setTimeout ( ( ) => {
412+ if ( ! popup . closed ) {
413+ popup . close ( ) ;
414+ }
415+ } , 300 ) ;
416+ }
417+
386418 const callbackError = getSocialAuthCallbackError ( e ?. data ) ;
387419 if ( callbackError ) {
388420 setIsSocialAuthLoading ( false ) ;
@@ -556,6 +588,21 @@ function AuthOptionsInner({
556588 windowPopup . current . location . href = socialUrl ;
557589 await setChosenProvider ( provider ) ;
558590 onAuthStateUpdate ?.( { isLoading : true } ) ;
591+
592+ authFlowCompletedRef . current = false ;
593+ clearPopupCheck ( ) ;
594+ const popup = windowPopup . current ;
595+ popupCheckIntervalRef . current = setInterval ( ( ) => {
596+ if ( ! popup || popup . closed ) {
597+ clearPopupCheck ( ) ;
598+ if ( ! authFlowCompletedRef . current ) {
599+ setIsSocialAuthLoading ( false ) ;
600+ onAuthStateUpdate ?.( { isLoading : false } ) ;
601+ displayToast ( SOCIAL_AUTH_RETRY_MESSAGE ) ;
602+ }
603+ windowPopup . current = null ;
604+ }
605+ } , 500 ) ;
559606 } ;
560607
561608 const onProviderMessage = async ( e : MessageEvent ) => {
0 commit comments