[CORD-1182] Fixed login error, prevent app to reach dashboard

Change-Id: I7eae22b11820ec0fb43057fddcebc8292871665b
diff --git a/src/app/core/loader/loader.spec.ts b/src/app/core/loader/loader.spec.ts
index 1bcff9c..d4b88dd 100644
--- a/src/app/core/loader/loader.spec.ts
+++ b/src/app/core/loader/loader.spec.ts
@@ -3,6 +3,7 @@
 import {xosLoader} from './loader';
 
 let loaded = true;
+let authenticated = true;
 
 const MockConfig = {
   lastVisitedUrl: '/test'
@@ -17,6 +18,15 @@
   onboard: null
 };
 
+const MockAuth = {
+  isAuthenticated: jasmine.createSpy('isAuthenticated')
+    .and.callFake(() => authenticated)
+};
+
+const MockState = {
+  go: jasmine.createSpy('state.go')
+};
+
 describe('The XosLoader component', () => {
   beforeEach(() => {
     angular
@@ -24,6 +34,8 @@
       .value('XosConfig', MockConfig)
       .value('XosModelDiscoverer', MockDiscover)
       .value('XosOnboarder', MockOnboarder)
+      .value('AuthService', MockAuth)
+      .value('$state', MockState)
       .component('xosLoader', xosLoader);
     angular.mock.module('loader');
   });
@@ -99,6 +111,24 @@
     });
   });
 
+  describe('when user is not authenticated', () => {
+
+    beforeEach(() => {
+      loaded = false;
+      authenticated = false;
+      compileElement();
+      isolatedScope.run();
+    });
+
+    it('should redirect to the login page', () => {
+      expect(MockState.go).toHaveBeenCalledWith('xos.login');
+    });
+
+    afterEach(() => {
+      authenticated = true;
+    });
+  });
+
   describe('when models are not loaded', () => {
 
     beforeEach(() => {
diff --git a/src/app/core/loader/loader.ts b/src/app/core/loader/loader.ts
index 07a9875..f12b17b 100644
--- a/src/app/core/loader/loader.ts
+++ b/src/app/core/loader/loader.ts
@@ -1,23 +1,26 @@
 import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service';
 import {IXosOnboarder} from '../../extender/services/onboard.service';
+import {IXosAuthService} from '../../datasources/rest/auth.rest';
 class LoaderCtrl {
   static $inject = [
     '$log',
     '$rootScope',
     '$location',
     '$timeout',
+    '$state',
+    'AuthService',
     'XosConfig',
     'XosModelDiscoverer',
     `XosOnboarder`
   ];
 
-  public aaaaa = 'ciao';
-
   constructor (
     private $log: ng.ILogService,
     private $rootScope: ng.IScope,
     private $location: ng.ILocationService,
     private $timeout: ng.ITimeoutService,
+    private $state: ng.ui.IStateService,
+    private XosAuthService: IXosAuthService,
     private XosConfig: any,
     private XosModelDiscoverer: IXosModelDiscovererService,
     private XosOnboarder: IXosOnboarder
@@ -30,6 +33,9 @@
     if (this.XosModelDiscoverer.areModelsLoaded()) {
       this.moveOnTo(this.XosConfig.lastVisitedUrl);
     }
+    else if (!this.XosAuthService.isAuthenticated()) {
+      this.$state.go('xos.login');
+    }
     else {
       this.XosModelDiscoverer.discover()
       // NOTE loading XOS Models
@@ -48,7 +54,6 @@
         })
         .finally(() => {
           // NOTE it is in a timeout as the searchService is loaded after that
-          // we navigate to another page
           this.$timeout(() => {
             this.$rootScope.$emit('xos.core.modelSetup');
           }, 500);