After defining an Angular service where I need to access a cookie, I noticed that the $cookieStore is deprecated
and that it's recommended to use $cookies
instead. This is how my service is set up:
'use strict';
var lunchrServices = angular.module('lunchrServices', ['ngCookies']);
lunchrServices.service('authService', ['$cookies', function ($cookies) {
var user = $cookies.get('user') || null;
this.login = function (userEmail) {
user = userEmail;
$cookies.put('user', userEmail)
};
this.logout = function () {
user = null;
$cookies.put('user', null);
};
this.currentUser = function(){
return user;
}
}]);
However, when I try to retrieve the user with $cookies.get('user')
, I get an error saying
TypeError: undefined is not a function
. Strangely, if I switch every instance of $cookies
to $cookieStore
, it works without any issues:
'use strict';
var lunchrServices = angular.module('lunchrServices', ['ngCookies']);
lunchrServices.service('authService', ['$cookieStore', function ($cookieStore) {
var user = $cookieStore.get('user') || null;
this.login = function (userEmail) {
user = userEmail;
$cookieStore.put('user', userEmail)
};
this.logout = function () {
user = null;
$cookieStore.put('user', null);
};
this.currentUser = function(){
return user;
}
}]);
I would prefer to use $cookies
over $cookieStore
, but I'm puzzled as to why it's failing for one and not the other. Any insights on this issue would be greatly appreciated.