I have a authentication service that broadcasts a event when login is successful. Upon successful authentication I need to call a authorization service to retrieve the claims. The service that I have has a login function that I would like to return a promise. Upon successful login I want to call the getclaims() which will return the claims after a successful http call.
In my getclaims function when I try to access the 'deferred' variable I get a undefined.
Essentially I have a function1 on my service that calls into a 3rd party service. This 3rd party service broadcasts an event upon success/failure. I handle this event in my service. My success handler should call into function2 in my service. What I want to do is return a promise from my function1 where the consumer of my service can use a .then to chain the successful login.
(function () {
'use strict';
var factoryName = 'authService';
var dependencies = ['$http', 'configSettingService', '$rootScope',  '$log', '$q'];
function secFactory($http, configSettingService, $rootScope, adalAuthenticationService, $log, $q) {
    var deferred;
    function getclaims() {
        $http.get("someurl")
        .success(function (data, status, headers, config) {
           deferred.then(function success(data) {
                $rootScope.$broadcast('secService:loginSuccess');
            });
        }).
            error(function (data, status, headers, config) {
                $log.debug('login failure');
                $rootScope.$broadcast('secService:loginFailure');
            });
    }
    function login() {
        adalAuthenticationService.login();
        deferred = $q.defer();
        return deferred.promise;
    }
    $rootScope.$on('adal:loginSuccess', function (ev, user) {
        console.log('login-success');
        getclaims();
    });
    var service = {
        login: login,
        logout: logout,
    };
    return service;
}
var app = angular.module('CoreService');
app.factory(factoryName, secFactory);
secFactory.$inject = dependencies;})();
