I know this is an old thread, but this SO question kept popping up when I was Googling (yes, I just used Google as a verb ... handle this: P) for resolution, so I thought I should provide my solution . Hope this helps the OP or anyone else who may come across this page.
angular.module("app").factory("UserService", [ "$rootScope", "$state", "$q", "Restangular", function ($rootScope, $state, $q, Restangular) { var UserSvc = {}; var Identity; var UsersAPI = Restangular.withConfig(function (RestangularConfigurer) { RestangularConfigurer.setBaseUrl("api/1.0/users"); }); UserSvc.login = function (credentials) { var $defer = $q.defer(); UsersAPI.all("start-session").post(credentials).then(function(respData){ if (respData.apikey) { Identity = respData.plain(); Restangular.configuration.defaultRequestParams.common.apikey = Identity.apikey; if ($rootScope.toState && $rootScope.toState.name != "login") { $state.go($rootScope.toState.name, $rootScope.toStateParams || {}); } else { $state.go("app.dashboard"); } $defer.resolve(Identity); } else { Identity = undefined; $defer.reject(Identity); } },function (respData) { $defer.reject(respData); }); return $defer.promise; }; return UserSvc; } ]);
source share