title: Right way to check sessionStorage and pass variable to $scope in Angular?
body: <p>I have created a token-based authentication system, but need a way for Angular to know if a user is signed in or not.</p>
  
  <p>A signed in user will have there auth token in session storage. I'd like to set a variable, like <code>$scope.isLoggedIn = true</code>, if <code>if ( $window.sessionStorage.token )</code> is true.</p>
  
  <p>Then I'll use <code>ng-if="isLoggedIn"</code> to show and hide elements on the page as needed. Of course, the token is authenticated before any personal info is shown. This is basically a way of showing or hiding the landing page.</p>
  
  <p>I could do this in a controller, but I would have to do it in each controller. Can I do it with a factory?</p>
  
  <p>Here is what I tried, but is there a better way? Thanks.</p>
  
  <h2>In <code>app.config</code>:</h2>
  
  <pre><code>$httpProvider.interceptors.push('authCheck')
  </code></pre>
  
  <h2>In <code>app.factory</code>:</h2>
  
  <pre><code>//
  // set a scope variable for whether a user is signed in or not
  //
  .factory('authCheck', [ '$window', '$rootScope', function ( $window, $rootScope ) {
          $rootScope.isLoggedIn = false;
  
          if ( $window.sessionStorage.token ) {
              $rootScope.isLoggedIn = true;
          }
  
          return $rootScope.isLoggedIn;
  }])
  </code></pre>