title: AngularJs auth service behaves strangely
body: <p><em>Updated</em>: <a href="https://gist.github.com/zishe/6972512" rel="nofollow">code in JS</a></p>
  
  <p>I borrowed auth (session) service from <a href="https://github.com/angular-app/angular-app" rel="nofollow">angular-app</a>, and strongly modify it:</p>
  
  <pre><code>  angular.module('auth', [])
      .factory('session', ['$location', '$http', '$q', ($location, $http, $q) -&gt;
  
        service =
  
          requestCurrentUser: -&gt;
            if service.isAuthenticated()
              $q.when service.currentUser
            else
              $http.get('/user').then (response) -&gt;
                console.log(response.data)
                service.currentUser = response.data
                service.currentUser
  
          currentUser: null
  
          isAuthenticated: -&gt;
            not service.currentUser?
  
        service
      ])
  </code></pre>
  
  <p>This is an app:</p>
  
  <pre><code>  BookReader = angular.module('BookReader', ['ngRoute', 'home', 'main', 'books', 'auth'])
  
    BookReader
      .config(['$routeProvider', '$httpProvider', '$locationProvider', ($routeProvider, $httpProvider, $locationProvider) -&gt;
        $routeProvider
          .when '/',
            templateUrl: 'assets/tour.html'
            controller: 'MainCtrl'
          .when '/home',
            templateUrl: 'assets/main.html'
            controller: 'HomeCtrl'
          .when '/books',
            templateUrl: 'assets/books.html'
            controller: 'BooksCtrl'
      ])
      .run(['session', (session) -&gt;
        session.requestCurrentUser()
      ])
  </code></pre>
  
  <p>So, i guess, when the application starts the currentUser is requested (perhaps a controller starts working before the callback of the request, but the problem in other place). Next, Controllers:</p>
  
  <pre><code>angular.module('main', ['ngCookies'])
    .controller('MainCtrl', ['$scope', '$http', '$cookies', '$location', 'session', '$log',
      ($scope, $http, $cookies, $location, session, $log) -&gt;
        $log.info "currentUser: " + session.currentUser + " ; isAuth: " + session.isAuthenticated()
        $location.path '/home' if session.isAuthenticated()
    ])
  
  
  angular.module('home', ['ngCookies'])
    .controller('HomeCtrl', ['$scope', '$http', '$cookies', 'session', '$location', '$log'
      ($scope, $http, $cookies, session, $location, $log) -&gt;
        $log.info "currentUser: " + session.currentUser + " ; isAuth: " + session.isAuthenticated()
        $location.path '/' unless session.isAuthenticated()
    ])
  </code></pre>
  
  <p>The point is that <code>isAuthenticated()</code> always <code>false</code>, but <code>currentUser</code> not null when a user is logged in. So, when i logged in <code>$log.info</code> shows <code>currentUser: [object Object] ; isAuth: false</code> and not only when the site just loaded, but when i'm navigating too. When i logged out it shows <code>currentUser: null ; isAuth: false</code>. Obviously, <code>currentUser</code> is always null inside the service. But how can i define it to be able change the value?</p>