title: Angularjs resolve promise on .run and pass to controllers
body: <p>i have some troubles with a API-Side login and Angular.</p>
  
  <p><strong>The Scenario:</strong> </p>
  
  <p>Api Response at GET api/auth/token and, if exists and open sesion, return token and user data, else return false. API Response at POST api/auth/login for credentials and, if ok, return token and user data. </p>
  
  <p>The frontend need to load api/auth/token first, if the response is false, go to the login form, else, load the user data and the session token for using it in controllers.</p>
  
  <p><strong>What have i done:</strong></p>
  
  <p>Everything works well, but i can`t use userdata in controllers. </p>
  
  <p><strong>The Source</strong></p>
  
  <p>app.run</p>
  
  <pre><code>app.run(function (ApiService, $state, $location) {
      if($location.url() != '/login'){
          ApiService.token().then(function (data) {
              if (data.data.hasOwnProperty('user')) {
                  ApiService.user = data.data.user;
                  ApiService.token = data.data.token;
                  ApiService.permissions = data.data.permissions;
                  ApiService.auth = true;
              } else {
                  $state.go('login');
              }
          });
      }
  });
  </code></pre>
  
  <p>app.factory</p>
  
  <pre><code>app.factory('ApiService', ['$http', function ($http) {
      var api = {
          'api': 'api/v1/',
          'auth': false,
          'request': null,
          'token': null,
          'user': null,
          'permissions': null
      };
      api.token = function () {
          return $http.get(api.api + 'auth/token').success(function () {
          });
      };
      api.logIn = function (credentials) {
          return $http.post(api.api + 'auth/try', credentials).success(function () {
          });
      };
      api.logOut = function () {
          return $http.get(api.api + 'auth/logout').success(function () {
          });
      };
      return api;
  }]);
  </code></pre>
  
  <p>With that, in MainCtrl, using console.log(ApiService.user) i can see all the user data, but if i want to assign it to a $scope with $scope.user = ApiService.user, i dont see anything.</p>