1

I'm storing a user object on a Cookie and, when the client visit the site again, I want to use this object's properties.

But, when the client comes back, my cookie object looks like [object object] while it's properties looks like undefined.

Here is my code:

$scope.signup = function (user) {
    $http.post('/api/signup', user)
        .then( function (res) {
            $cookies.put('user', res.data.user); //This is where I store my cookie.
        }, function (res) {

        });
};

When the client comes back, I have this code:

var lastUser = $cookies.get('user');
if (lastUser) $scope.lastName = lastUser.username;
console.log(lastUser);  // [object object]
console.log(lastName); // undefined

What can I do to properly get the cookie info?

Rodmentou
  • 1,572
  • 3
  • 18
  • 37

1 Answers1

2

Try instead:

window.localStorage.setItem('user', JSON.stringify(res.data.user));

Then you can get that information on consequent visits with:

var userData = JSON.parse(window.localStorage.getItem('user'));

That way you'll avoid a lot of the cookie headache.

Hunan Rostomyan
  • 2,072
  • 2
  • 19
  • 29