0

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;})();
kolhapuri
  • 1,581
  • 4
  • 20
  • 31
  • 2
    If you have a strict sequence of operations, you probably want `fn1().then(fn2).then(fn3)` where each function returns it's own promise that is resolved when it is done. In your code, it is the `login()` function that sets `deferred` to have a value, but I don't see how that ever gets called or used. – jfriend00 Apr 28 '15 at 04:06
  • what is `adalAuthenticationService.login()`? Is this an async call? Is this returning a promise of its own? How do you know otherwise that the call returned? – New Dev Apr 28 '15 at 04:11
  • thats the very reason why you should use $q, it chains the promises.. one after the other... – harishr Apr 28 '15 at 04:45
  • @NewDev adalAuthenticationService.login() is a 3rd party service identity service provider. This service broadcasts the 'adal:loginSuccess' upon successful login – kolhapuri Apr 28 '15 at 11:02
  • @jfriend00, Yes there is a sequence of events. The controller calls the login method. When the 'adal:loginSuccess' is broadcasted/handled the getClaims needs to be called. The 'adal:loginSuccess' is broadcast by a 3rd party service which I do not have control over. – kolhapuri Apr 28 '15 at 11:15

0 Answers0