Merge "Fixing login issue when session expires (or containers are rebuilt)"
diff --git a/src/app/views/dashboard/dashboard.ts b/src/app/views/dashboard/dashboard.ts
index 45bd138..285c2df 100644
--- a/src/app/views/dashboard/dashboard.ts
+++ b/src/app/views/dashboard/dashboard.ts
@@ -18,37 +18,53 @@
 
 import {IXosModelStoreService} from '../../datasources/stores/model.store';
 import {IXosAuthService} from '../../datasources/rest/auth.rest';
+import {Subscription} from 'rxjs/Subscription';
+
+
 class DashboardController {
-  static $inject = ['$scope', '$state', 'XosModelStore', 'AuthService'];
+  static $inject = [
+    '$log',
+    '$scope',
+    '$state',
+    'XosModelStore',
+    'AuthService'
+  ];
 
   public nodes: number;
   public slices: number;
   public instances: number;
 
+  private nodeSubscription: Subscription;
+  private sliceSubscription: Subscription;
+  private instanceSubscription: Subscription;
+
   constructor(
+    private $log: ng.ILogService,
     private $scope: ng.IScope,
     private $state: ng.ui.IStateService,
     private store: IXosModelStoreService,
     private auth: IXosAuthService
   ) {
 
+    this.$log.info(`[XosDashboardView] Setup`);
+
     if (!this.auth.isAuthenticated()) {
       this.$state.go('login');
     }
     else {
-      this.store.query('Node')
+      this.nodeSubscription = this.store.query('Node')
         .subscribe((event) => {
           this.$scope.$evalAsync(() => {
             this.nodes = event.length;
           });
         });
-      this.store.query('Instance')
+      this.instanceSubscription = this.store.query('Instance')
         .subscribe((event) => {
           this.$scope.$evalAsync(() => {
             this.instances = event.length;
           });
         });
-      this.store.query('Slice')
+      this.sliceSubscription = this.store.query('Slice')
         .subscribe((event) => {
           this.$scope.$evalAsync(() => {
             this.slices = event.length;
@@ -59,6 +75,12 @@
       this.slices = 0;
     }
   }
+
+  $onDestroy () {
+    this.nodeSubscription.unsubscribe();
+    this.instanceSubscription.unsubscribe();
+    this.sliceSubscription.unsubscribe();
+  }
 }
 
 export const xosDashboard: angular.IComponentOptions = {
diff --git a/src/index.ts b/src/index.ts
index 2a287d5..68920b0 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -83,11 +83,12 @@
   .component('xos', main)
   .provider('XosConfig', function(){
     // save the last visited state before reload
-    const lastVisitedUrl = window.location.hash.replace('#', '');
+    let lastVisitedUrl = window.location.hash.replace('#', '');
     this.$get = [() => {
-      return {
-        lastVisitedUrl
-      };
+      if (lastVisitedUrl === '/login' || lastVisitedUrl === '/loader') {
+        lastVisitedUrl = '/dashboard';
+      }
+      return {lastVisitedUrl};
     }] ;
     return this;
   })
@@ -129,11 +130,15 @@
     // if the user is authenticated
     $log.info(`[XOS] Is user authenticated? ${AuthService.isAuthenticated()}`);
     if (AuthService.isAuthenticated()) {
+      $log.info(`[XOS] Redirect to "loader"`);
       $state.go('loader');
+      $rootScope.$apply();
     }
     else {
       AuthService.clearUser();
+      $log.info(`[XOS] Redirect to "login"`);
       $state.go('login');
+      $rootScope.$apply();
     }
 
     // register keyboard shortcut