Angular.js - $cookieStore null in promise callback

792 views Asked by At

I'm trying to get my head around Angular.js and can't seem to resolve one problem that I'm having.

I have the following code:

'use strict';
angular.module('authValidator',['ngCookies', 'ngResource']).factory('$authValidator', ['$cookieStore', '$http', function($cookieStore, $http){
return {
    setUserToken: function(token){
        $cookieStore.set('currentUserToken', token);
    },
    userToken: function(){
        $cookieStore.get('currentUserToken');
    },
    loggedIn: function(){
        return false;
    },
    requestAuthentication:function(username, password){
        var self = this;
        return $http.post('/login', {"username":username, "password":password}).then(function(response) {
            self.setUserToken(data.security_token);
        });
    },
    clearUserToken: function(){
        $cookieStore.remove('currentUserToken');
    }
}
}]);

The problem that I'm having is that $cookieStore appears to be null inside the 'then' and as a result I encounter this error:

Error: 'undefined' is not a function (evaluating '$cookieStore.set('currentUserToken', token)')

I know there is an incredibly basic thing that I'm missing, but I can't seem to find it. Any recommendations are greatly appreciated.

Thanks

1

There are 1 answers

1
John Tseng On BEST ANSWER

I think the problem is that $cookieStore has a put, but no set.