AngularJs Jwt Auth

时间:2016-05-20 06:30:29

标签: angularjs jwt

我在AngularJs中设置了一个拦截器。在这里,我在响应中收到一个令牌。我在我的请求中发送一个。但是,当我刷新2次时它变得如此之快以至于我退出了。

employeeAppModule.config([
        '$httpProvider',
        function ($httpProvider) {
            $httpProvider.interceptors.push(function () {
                var token, headers, $cookies;

                //inject cookies
                angular.injector(['ngCookies']).invoke(['$cookies', function(_$cookies_) {
                    $cookies = _$cookies_;
                }]);

                return {
                    request: function (request) {
                        token = $cookies.get('jwt-token');
                        headers = request.headers || (request.headers = {});
                        request.headers = headers;
                        if(token != null && token != 'undefined') {
                            request.headers.Authorization = 'Bearer' + token;
                        }

                        return request;
                    },
                    response: function (response) {
                        if (typeof response.data.result === 'undefined') {
                            return response;
                        }

                        if(response.status && response.status.code === 401) {
                            alert('token wordt verwijderd');
                        }

                        if(response.data && response.data.result.token && response.data.result.token.length > 10) {
                            $cookies.put('jwt-token', response.data.result.token);
                        }
                        return response;
                    }
                };
            });
        }]);

0 个答案:

没有答案