Best way to manage synchronous server side data in AngularJS

I'm using Oauth to manage session users in my AngularJS app. My Angular app needs to know information about the session user because it is used in my $resource objects(to make REST calls like GET /user/:user/projects). I want this user variable to be global and synchronous, so making an async call with $resource or $http to get the user adds boilerplate to the code that needs this session user(because it has to wait for the user information to return from server).

Right now I'm using this dirty hack that resolves the issue: in my index.ejs file(notice that the user is rendered server side with ejs)

    <script type="text/javascript">

    var session_user =  <%- user  %>;

    </script>

and then I do

$rootScope.user = session_user

The problem is that when I unit test the controller, of course session_user is not defined.

I'm thinking that maybe I could create a service that returns this session_user variable, so I can use a mock for testing. Any other ideas? What is the recommended aproach to this kind of problems?

It doesn't seem like that much of dirty hack actually. It would seem problematic only if the session user would change without reloading your html from the server (and thereby getting a new session user).

Angular modules have a constant function that I use for this sort of thing:

angular.module("MyModule", [])
       .constant("User", window.session_user);

And then you can treat User as a injectable value in your controllers.