title: How to prevent the need login everytime without logout in Ionic App?
body: <p>How I can make my Ionic App to prevent the need login everytime after exit app or back to background without logout ?
  Its my source code:</p>
  
  <p><strong>login.js</strong></p>
  
  <pre><code>   angular.module('starter.controllers')
  
     .controller('LoginCtrl', ['$scope', '$rootScope', '$location', '$localStorage', '$q', '$http', '$window', '$state', 'AuthFac tory', 'SessionFactory',
         function($scope, $rootScope, $location, $localStorage, $q, $http, $window, $cordovaSQLite, $state, AuthFactory, SessionFactory) {
  
             $scope.login = {
                 email: null,
                 password: null
             };
  
             $scope.login = function() {
                 $rootScope.showLoading("Authenticating..");
  
                 var email = $scope.login.email,
                     password = $scope.login.password;
  
                 if (email !== undefined &amp;&amp; password !== undefined) {
                     AuthFactory.login(email, password).success(function(data, status, headers, config) {
                         if (data.success === false) {
                             $rootScope.hideLoading();
                             $rootScope.toast('Invalid Credentials');
                         } else {
                             SessionFactory.isLogged = true;
                             SessionFactory.user = data.data.username;
                             SessionFactory.userRole = data.data.name;
                             $localStorage.id = data.data.id;
                             $localStorage.token = data.data.token;
                             $window.sessionStorage.token = data.data.token;
                             console.log($window.sessionStorage.token);
                             $localStorage.user = data.data.username; // to fetch the user details on refresh
                             console.log($localStorage.user);
                             $localStorage.userRole = data.data.name; // to fetch the user details on refresh
                             console.log($localStorage.userRole);
                         } //end else data.success
                     }).error(function(data, status, headers, config) {
                         if (status === 500) {
                             $rootScope.hideLoading();
                             $rootScope.toast('Invalid Email');
                         }
                     });
                 }
             };
         }
     ]);
  </code></pre>
  
  <p><strong>authfactory.js</strong></p>
  
  <pre><code>angular.module('starter.factories')
  
  .factory('SessionFactory', function($window, $localStorage) {
    var auth = {
      isLogged: false,
      check: function() {
        if ($localStorage.token &amp;&amp; $localStorage.user ) {
          this.isLogged = true;
        } else {
          this.isLogged = false;
          delete this.user;
        }
      }
    }
  
  
    return auth;
  })
  
  
      .factory('AuthFactory', function($window, $location, $http, SessionFactory, $localStorage) {
        return {
          login: function(email, password) {
            return $http.post('url', {
              email : email,
              password: password
            });
  
          },
  
          logout: function() {
  
            if (SessionFactory.isLogged) {
  
              SessionFactory.isLogged = false;
              delete SessionFactory.user;
              delete SessionFactory.userRole;
  
              delete $localStorage.token;
              delete $localStorage.user ;
              delete $window.sessionStorage.userRole;
  
              $location.path("/login");
            }
  
          }
        }
      })
  
  
      .factory('TokenInterceptor', function($q, $window, $localStorage) {
        return {
          request: function(config) {
            config.headers = config.headers || {};
            if ($localStorage.token) {
              config.headers['X-Access-Token'] = $localStorage.token;
              config.headers['X-Key'] = $localStorage.user;
              config.headers['Content-Type'] = "application/json";
            }
            return config || $q.when(config);
          },
  
          response: function(response) {
            return response || $q.when(response);
          }
        };
      })
  </code></pre>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>angular.module('starter.controllers', []);
  angular.module('starter.services', []);
  angular.module('starter.factories', []);
  
  angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'starter.factories', 'ngCordova', 'ngRoute'])
  
  .run(function($ionicPlatform, $rootScope, $ionicLoading, $state, $location, $q, $http, $timeout, $localStorage, $window, SessionFactory) {
    $ionicPlatform.ready(function() {
  
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      if (window.cordova &amp;&amp; window.cordova.plugins &amp;&amp; window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      }
      if (window.StatusBar) {
        // org.apache.cordova.statusbar required
        StatusBar.styleLightContent();
      }
  
      $ionicPlatform.registerBackButtonAction(function(event) {
        if ($state.current.name == "tab.dashboard") {
          navigator.app.exitApp();
          console.log('1');
        } else {
          navigator.app.backHistory();
          console.log('2');
        }
      }, 100);
  
      if (window.cordova) {
  
  
      });
  
      // when the page refreshes, check if the user is already logged in
      SessionFactory.check(); $rootScope.showLoading = function(msg) {
        $ionicLoading.show({
          template: msg || 'Loading',
          animation: 'fade-in',
          showBackdrop: true,
          maxWidth: 200,
          showDelay: 0
        });
        $timeout(function() {
          $rootScope.hideLoading();
        }, 2999);
      }
  
      $rootScope.hideLoading = function() {
        $ionicLoading.hide();
      };
  
      $rootScope.toast = function(msg) {
        $rootScope.showLoading(msg);
        $timeout(function() {
          $rootScope.hideLoading();
        }, 2999);
      };
  
      $ionicPlatform.on('resume', function() {
        $rootScope.$on("$routeChangeStart", function(event, nextRoute, currentRoute) {
          if ((nextRoute.access &amp;&amp; nextRoute.access.requiredLogin) &amp;&amp; !SessionFactory.isLogged) {
            $location.path("/login");
          } else {
            // check if user object exists else fetch it. This is incase of a page refresh
            if (!SessionFactory.user) SessionFactory.user = $localStorage.user;
            if (!SessionFactory.userRole) SessionFactory.userRole = $localStorage.userRole;
          }
          console.log('true');
        });
  
        $rootScope.$on('$routeChangeSuccess', function(event, nextRoute, currentRoute) {
          $rootScope.showMenu = SessionFactory.isLogged;
          $rootScope.role = SessionFactory.userRole;
          // if the user is already logged in, take him to the home page
          if (SessionFactory.isLogged == true &amp;&amp; $location.path() == '/login') {
            $location.path('/tab/price');
          }
        });
        console.log(SessionFactory.isLogged);
        $rootScope.$broadcast('onResume');
      });
  
      $ionicPlatform.on('pause', function() {
        $rootScope.$broadcast('onPause');
        console.log('pause');
      });
  
  
    })
  
    .config(function($stateProvider, $urlRouterProvider, $httpProvider, $ionicConfigProvider) {
  
      $httpProvider.interceptors.push('TokenInterceptor');
      // Ionic uses AngularUI Router which uses the concept of states
      // Learn more here: https://github.com/angular-ui/ui-router
      // Set up the various states which the app can be in.
      // Each state's controller can be found in controllers.js
      $stateProvider
  
      // setup an abstract state for the tabs directive
      .state('login', {
        url: '/login',
        templateUrl: 'templates/login.html',
        controller: 'LoginCtrl',
        access: {
          requiredLogin: false
        }
      })
  
  
      // if none of the above states are matched, use this as the fallback
      $urlRouterProvider.otherwise('/login');
  
    });
  </code></pre>
  
  <p><strong>login.html</strong></p>
  
  <pre><code>&lt;ion-view view-title="Ionic App"&gt;
    &lt;ion-content&gt; 
      &lt;ion-list&gt;
        &lt;form name="loginForm"&gt;
          &lt;div class="list"&gt;
  
              &lt;label class="item item-input"&gt;
                  &lt;input type="email" placeholder="Email" ng-model="login.email"  required&gt;
              &lt;/label&gt;
              &lt;label class="item item-input"&gt;
                   &lt;input type="password" placeholder="Password" ng-model="login.password"  required&gt;
              &lt;/label&gt;
                &lt;button class="button button-block button-assertive" ng-click="login()" ng-disabled="loginForm.$invalid"&gt;
             &lt;b&gt;Login&lt;/b&gt;
             &lt;/button&gt;
          &lt;/div&gt;
        &lt;/form&gt;
      &lt;/ion-list&gt;
    &lt;/ion-content&gt;
  &lt;/ion-view&gt;         
  </code></pre>
  
  <p>I hope anyone can help me to solve this problem</p>