strongloop/loopback-angular — pull request #14 by bajtos (2014-02-06 14:46:43+00:00)
Title: Add $rootScope.$currentUser and $rootScope.$ensureCurrentUser()

Make it easier to access data of the user that is currently logged in.

Before this change, developers had to add bits of code all over the application.

```js
// Inside app run block
$rootScope.$on("$routeChangeStart", function(event, next, current) {
if (!$rootScope.currentUser && $location.path() != '/login') {
$rootScope.currentUser = User.getCurrent();
}
});

// Inside app config block
$httpProvider.interceptors.push(function($q, $location, $rootScope) {
return {
responseError: function(rejection) {
if (rejection.status == 401) {
$rootScope.currentUser = null;
// etc.
}
return $q.reject(rejection);
}
};
});

// In the login action
User.login({ include: 'user' }, $scope.credentials, function(result) {
$rootScope.currentUser = new User(result.user);
// etc.
});

// In the logout action
User.logout(function() {
$rootScope.currentUser = null;
// etc.
});
```

Once this patch is landed, working with the current user is super easy:

```js
// Inside app run block
$rootScope.$on("$routeChangeStart", function(event, next, current) {
if ($location.path != '/login')
$rootScope.$ensureCurrentUser();
});

// use $rootScope.$currentUser instead of $rootScope.currentUser
```

/to @ritch or @Schoonology please review.
/cc @seanbrookes
/cc @crandmck I'll update the docs after this patch is accepted.