$ cookies.putObject does not work in angular 1.4

I updated from angular 1.2 (source code) recently to 1.3 (which violated cookies) to 1.4 today ($ cookieStore is now deprecated due to $ cookies)

I added a line $cookies.putObject('user', user);, but it is not saved in cookies (I checked on the chrome resources tab)

        /**
         * Authenticate user
         *
         * @param  {Object}   user     - login info
         * @param  {Function} callback - optional
         * @return {Promise}
         */
        login: function (user, callback) {
            var cb = callback || angular.noop;

            return Session.save({
                email: user.email,
                password: user.password
            }, function (user) {
                $rootScope.currentUser = user;
                $cookies.putObject('user', user); //does not save

                return cb();
            }, function (err) {
                return cb(err);
            }).$promise;
        },

I set a breakpoint right after $cookies.putObject, and I get undefinedwhen I try $cookies.getObject('user')in the Chrome console.

This is a problem because now, when I refresh the page, I am losing the registered user. The service Auth.jshas this line in front:

    // Get currentUser from cookie
    $rootScope.currentUser = $cookies.getObject('user') || null;
+4
source share
1 answer

, angular ... . cookie 4093. 4207.

, , userId cookie..., .

localStorage, - , ( )... localStorage.

:

    // Get currentUser from cookie
    $rootScope.currentUser = $cookies.get('userId');

    User.get().$promise
        .then(function(user){
            $rootScope.currentUser = user._id && user || null;
        });
+1

Source: https://habr.com/ru/post/1599702/


All Articles