@@ -399,7 +399,14 @@ var loadProfile = function(landingRoute, onboarded) {
399399$ ( document ) . ajaxSend ( function ( e , jqXhr , settings ) {
400400 // With this we could map ajax responses to the server config
401401 // that was active when they were initiated.
402- jqXhr . serverConfig = app . serverConfigs . getActive ( ) . id ;
402+ jqXhr . serverConfig = app . serverConfigs . getActive ( ) ;
403+ } ) ;
404+
405+ $ ( document ) . ajaxError ( function ( event , jqxhr , settings , thrownError ) {
406+ if ( jqxhr . status === 401 && jqxhr . serverConfig . id === app . serverConfigs . getActive ( ) . id ) {
407+ app . serverConnectModal . failConnection ( 'failed-auth' , jqxhr . serverConfig )
408+ . open ( ) ;
409+ }
403410} ) ;
404411
405412launchOnboarding = function ( guidCreating ) {
@@ -447,24 +454,26 @@ pageConnectModal.on('cancel', () => {
447454app . connectHeartbeatSocket ( ) ;
448455app . serverConnectModal = new ServerConnectModal ( ) . render ( ) ;
449456app . serverConnectModal . on ( 'connected' , ( authenticated ) => {
450- $loadingModal . removeClass ( 'hide' ) ;
451-
452- if ( authenticated ) {
453- profileLoaded && location . reload ( ) ;
457+ if ( profileLoaded ) {
458+ // If we've already loaded called loadProfile() and then, we connect
459+ // to a new server (or reconnect to the same server) we'll reload the
460+ // app since some of the "global" components (Router, PageNav,
461+ // SocketView...) were not designed to handle a new connection.
462+ authenticated && location . reload ( ) ;
463+ } else {
464+ // clear some flags so the heartbeat events will
465+ // appropriatally loadProfile or launch onboarding
466+ guidCreating = null ;
467+ loadProfileNeeded = true ;
454468 app . serverConnectModal . close ( ) ;
469+ $loadingModal . removeClass ( 'hide' ) ;
455470 }
456471} ) ;
457472
458473app . getHeartbeatSocket ( ) . on ( 'open' , function ( e ) {
459474 removeStartupRetry ( ) ;
460475 pageConnectModal . remove ( ) ;
461476 $loadingModal . removeClass ( 'hide' ) ;
462- onboardingModal && onboardingModal . remove ( ) ;
463-
464- // clear some flags so the heartbeat events will
465- // appropriatally loadProfile or launch onboarding
466- guidCreating = null ;
467- loadProfileNeeded = true ;
468477} ) ;
469478
470479app . getHeartbeatSocket ( ) . on ( 'close' , ( startUpRetry = function ( e ) {
@@ -518,6 +527,7 @@ app.getHeartbeatSocket().on('message', function(e) {
518527 case 'online' :
519528 if ( loadProfileNeeded && ! guidCreating ) {
520529 loadProfileNeeded = false ;
530+ onboardingModal && onboardingModal . remove ( ) ;
521531
522532 app . login ( ) . done ( function ( data ) {
523533 if ( data . success ) {
0 commit comments