title: Generate Routes in .run() with config from service Angularjs
body: <p>I am building routes/states and the menu based on what the user is authorized to see. I've looked around and tried a few different things, but i'm hitting a brick wall.  The SessionService object in the RoleService Factory is empty whenever RoleService.validateRole() is called. No route is added and the app is effectively dead.  Why is the injected factory empty and the methods undefined.</p>
  
  <p>Here is a simplified layout of the app starting in order of dependencies.</p>
  
  <p>In app.run(), I am adding the states to the app instead of doing it in the config.</p>
  
  <pre><code>$stateProviderRef.state(value.stateName, state);
  </code></pre>
  
  <p>The states come from (a factory) AppConfig.getStates(), which returns an array.</p>
  
  <pre><code>var states = AppConfig.getStates();
  </code></pre>
  
  <p>In getStates() we validate each route's role.</p>
  
  <pre><code>if(RoleService.validateRole(routes[i].role))
  </code></pre>
  
  <p>The RoleService depends on the SessionService and the validateRole function does this check:</p>
  
  <pre><code>if(SessionService.currentUser.role === role)
  </code></pre>
  
  <p>The SessionService depends on the AuthenticationService which is just a factory that returns a promise using $http (the user object). The SessionService.currentUser is a function that .then()s the returned promise from the AuthenticationService.</p>
  
  <pre><code> return {
       currentUser: function(){
           AuthenticationService.then(function(result){
               return result;
           });
       }
   };
  </code></pre>
  
  <p>I'm not sure of a better way to explain the code without including the entire files.</p>