Merge "Preventing to visit the login page if the user is logged in"
diff --git a/src/app/core/login/login.ts b/src/app/core/login/login.ts
index c091e43..ac0da82 100644
--- a/src/app/core/login/login.ts
+++ b/src/app/core/login/login.ts
@@ -13,6 +13,11 @@
     private $state: angular.ui.IStateService,
     private ModelSetup: IXosModelSetupService
   ) {
+
+    if (this.authService.isAuthenticated()) {
+      this.$state.go('xos.dashboard');
+    }
+
     this.img = this.getImg(StyleConfig.background);
 
     this.loginStyle = {
diff --git a/src/app/core/services/runtime-states.ts b/src/app/core/services/runtime-states.ts
index bc99a8e..652b554 100644
--- a/src/app/core/services/runtime-states.ts
+++ b/src/app/core/services/runtime-states.ts
@@ -7,7 +7,10 @@
   this.$get = function($state: ng.ui.IStateService) {
     return {
       addState: function(name: string, state: IXosState) {
-        $stateProvider.state(name, state);
+        // prevent to add multiple time the same state
+        if (!$state.get(name)) {
+          $stateProvider.state(name, state);
+        }
       }
     };
   };
diff --git a/src/app/datasources/rest/auth.rest.ts b/src/app/datasources/rest/auth.rest.ts
index f963799..edf7705 100644
--- a/src/app/datasources/rest/auth.rest.ts
+++ b/src/app/datasources/rest/auth.rest.ts
@@ -23,6 +23,7 @@
   logout(): Promise<any>;
   getUser(): any; // NOTE how to define return user || false ???
   isAuthenticated(): boolean;
+  clearUser(): void;
 }
 export class AuthService {
 
@@ -56,9 +57,7 @@
       xossessionid: this.$cookies.get('xossessionid')
     })
       .then(() => {
-        this.$cookies.remove('xoscsrftoken');
-        this.$cookies.remove('xossessionid');
-        this.$cookies.remove('xosuser');
+        this.clearUser();
         d.resolve();
       })
       .catch(e => {
@@ -67,6 +66,12 @@
     return d.promise;
   }
 
+  public clearUser(): void {
+    this.$cookies.remove('xoscsrftoken');
+    this.$cookies.remove('xossessionid');
+    this.$cookies.remove('xosuser');
+  }
+
   public getUser(): IXosUser {
     const user = this.$cookies.get('xosuser');
     if (angular.isDefined(user)) {
diff --git a/src/interceptors.ts b/src/interceptors.ts
index 80be130..273d19e 100644
--- a/src/interceptors.ts
+++ b/src/interceptors.ts
@@ -9,10 +9,13 @@
   $resourceProvider.defaults.stripTrailingSlashes = false;
 }
 
-export function userStatusInterceptor($state: angular.ui.IStateService) {
+export function userStatusInterceptor($state: angular.ui.IStateService, $cookies: ng.cookies.ICookiesService) {
 
   const checkLogin = (res) => {
     if (res.status === 401 || res.status === -1) {
+      $cookies.remove('xoscsrftoken');
+      $cookies.remove('xossessionid');
+      $cookies.remove('xosuser');
       $state.go('login');
     }
     return res;