【问题标题】:Angular $cookies not persistingAngular $cookies 不持久
【发布时间】:2023-04-05 19:04:01
【问题描述】:

我正在使用 angular $resource 来访问 API。在这种特殊情况下,使用自定义登录操作,以便登录用户。

为了在身份验证后保存 API 提供的令牌,我将一个拦截器附加到自定义登录操作,它只是通过 $cookies 将令牌保存到 cookie。

我正在使用拦截器,因为我需要从应用程序的其他部分保存和更新该 cookie。

问题在于 cookie 似乎没有持续存在。

如果我在保存后检查该值,我可以确认它在那里,但只要我的 $location 更改(例如在登录后呈现主页时),cookie 就会消失。

有什么想法吗?

谢谢,D。

这是我的登录服务,它使用自定义登录操作和拦截器

(function() {
    'use strict';

    //Login Service
    // Authenticates a user
    angular
    .module('console')
    .factory('LoginService', LoginService);    
        LoginService.$inject = ['$resource', 'Config', 'UserInfoInterceptor'];

            function LoginService($resource, Config, UserInfoInterceptor) {

                return $resource(Config.api_url + '/auth/', {},
                    {
                        login: {
                        method: 'POST',
                        url: Config.api_url + '/auth/login',
                        data: {type: "passwd"},
                        interceptor: UserInfoInterceptor,
                        headers: {
                        "accept": "application/json",
                        "content-Type": "application/json;odata=verbose"
                    }
                }
            });
    }
})();

这里是拦截器(那里的那个console.log,实际上写入了cookie值),但是在转到其他位置后它就丢失了。

(function() {
  'use strict';

  angular
    .module('console')
    .factory('UserInfoInterceptor', UserInfoInterceptor);
    UserInfoInterceptor.$inject = ['$resource', 'Config', '$q', '$location', '$cookies'];


    function UserInfoInterceptor($resource, Config, $q, $location, $cookies) {    

        return {

                // Save the received token 
                'response': function (response) {

                    var now = new Date();
                    // this will set the expiration to 30 days
                    var exp = new Date(now.getFullYear(), now.getMonth()+1, now.getDate());

                    // Set the cookie
                    $cookies.put('token', response.data.token, {expires: exp});

                    //This actually works!!
                    console.log($cookies.get('token'));

                    return response;
                },

                // Whatever is the failure, throw the user to the login page
                'responseError': function (response) {
                    if (response.status === 401 || response.status === 403) {
                        $location.path('/login');
                    }
                    return $q.reject(response);
                }
            };    
    }

【问题讨论】:

    标签: javascript angularjs cookies interceptor angular-resource


    【解决方案1】:

    这为时已晚,但我认为此修复可能对其他人也有帮助。

    我尝试在$cookiesProvider中设置cookie路径如下图,

    xxApp.config(['$cookiesProvider', function ($cookiesProvider) {
         $cookiesProvider.defaults.path = '/'; 
    }]);
    

    【讨论】:

    • 永远不会太晚
    猜你喜欢
    • 2014-09-02
    • 1970-01-01
    • 2021-05-29
    • 2019-05-10
    • 2018-05-03
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多