Oam Psy
Oam Psy

Reputation: 8663

Creating a simple Angular promise after service call

I am new to Angular promises and really trying to get my head around it.

I have a Controller (MainController) that attempts to authenticate a user via myAuthSrv.authUser.. simple username and password. On .success, i am creating a cookie via myCookieSrv.createCookie.

What i want to do after creating a cookie is successful, is to call another service - myAuthSrv.validateUser

Controller:

app.controller('MainController', function($scope, $http, myCookieSrv, myAuthSrv) {

    myAuthSrv.authUser($scope.data)
        .success(function (data, status, headers, config) {
                var cookieName = 'myCookieName';
                myCookieSrv.createCookie(cookieName, Id, 1);
                    //////////////////////////////////////////////
                    //.then(console.log("call promise here!!!"))
                    //.catch(error)
                    //////////////////////////////////////////////
            }
        })
        .error(function (data, status, headers, config) {
            $scope.err = "Something has gone wrong";
        })
});

myCookieSrv:

app.service('myCookieSrv', function() {
    return {
        createCookie : function(cookieName, Id, days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
            }
            else var expires = "";
            document.cookie = cookieName+"="+Id+expires+"; path=/";

        },
        readCookie : function(cookieName) {
            var nameEQ = cookieName + "=";
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
            }
            return null;
        }
    }
});

After myCookieSrv.createCookie has been successfully executed, i want to call validateUser from myAuthSrv:

app.service('myAuthSrv', function($http) {
    this.authUser = function() {
        return $http({
            method: 'POST',
            url: 'some url',
            data: {},
            headers: {'Content-Type': 'application/json'}
        });
    }

    this.validateUser = function() {
        return $http({
            method: 'POST',
            url: 'some url',
            data: {},
            headers: {'Content-Type': 'application/json'}
        });
    }  
}

Upvotes: 0

Views: 81

Answers (3)

Nabin Singh
Nabin Singh

Reputation: 377

This should help.

app.controller('MainController', function($scope, $http, myCookieSrv, myAuthSrv) {
myAuthSrv.authUser($scope.data)
    .success(function (data, status, headers, config) {
            var cookieName = 'myCookieName';
            myCookieSrv.createCookie(cookieName, Id, 1);
                myAuthSrv.validateUser()
                .success(function(){
                    //Auth Success
                })
                .error(function(){
                    //Error in auth
                });
        }
    })
    .error(function (data, status, headers, config) {
        $scope.err = "Something has gone wrong";
    })
});

Upvotes: 0

Nithin A
Nithin A

Reputation: 374

Do something like this

var promise = loginService
                                .logincall($scope.credentials);

                        promise
                                .then(
                                        function(payload) {
                                            //write your code (payload is the data returned)
});

Upvotes: 2

Mikalai
Mikalai

Reputation: 1525

You have not to create promise after creating cookie. Just change controller to:

app.controller('MainController', function($scope, $http, myCookieSrv, myAuthSrv) {

    myAuthSrv.authUser($scope.data)
        .then(function () {
            var cookieName = 'myCookieName';
            myCookieSrv.createCookie(cookieName, Id, 1);
        })
        .then(function () {
           return myAuthSrv.validateUser();
        })
        .then(function () {
           //success
         }, function () {
            $scope.err = "Something has gone wrong";
        })
});

Upvotes: 4

Related Questions