0

I implemented jwt stateless in backend , so except login and signup all other method has to intercept in angularjs and sending auth token to server in request header .But token is not sending mean not seeing in reqest header in console(developer tools).

This is my interceptor.js :

/**
 * 
 */
/* Interceptor declaration */
rootApp.factory('authInterceptor', function ($rootScope, $q, $sessionStorage, $location,$window) {
    return {
        request: function (config) {
            //config.headers['Content-Type'] = 'application/json';
            config.headers = config.headers || {};
            if ($window.sessionStorage.token) {
                config.headers.Authorization =  'Bearer '  + $window.sessionStorage.token;
                //config.headers['x-auth-token'] ='Bearer '  + $window.sessionStorage.token;
            }
            return config;
        },
        response: function (response) {

            if(response.status === 200){                
                if(response.data && response.data.success === false){
                    if($rootScope.authFailureReasons.indexOf(response.data.reason) !== -1){
                        $location.path('/login');
                    }
                }
            }

            if (response.status === 401) {
                $location.path('/');
            }

            return response || $q.when(response);
        },
        'responseError': function (rejection) {
            return $q.reject(rejection);
        }
    };
});

rootApp.config(['$httpProvider', function ($httpProvider) {
      // $httpProvider.interceptors.push('headerInterceptor');
        $httpProvider.interceptors.push('authInterceptor');
    }]);

And service.js file is:

rootApp.service('adminService', function ($rootScope, $http, $q,$window) {

    return {
        inviteUser: function (user) {

            var deferred = $q.defer();

            $http({
                method: 'POST',
                url:$rootScope.baseUrl+'api/v1/admin/user/add',
                data:user


            }).success(function (response, status, headers, config) {



                deferred.resolve(response);
            }).error(function () {
                // Something went wrong.
                deferred.reject({'success': false, 'msg': 'Oops! Something went wrong. Please try again later.'});
            });

            return deferred.promise;

        }
    };
});

In server side X-AUTH-TOKEN is allowed in headers too.Where i am going wrong Please help.

1
  • are you sure if ($window.sessionStorage.token) is evaluating to true? Commented Mar 18, 2016 at 12:05

1 Answer 1

1

hey I have recently made my first token authentication in my Ionic app .. yeah it is an easy step but implementation may take some time if you are not a language PRO.. you can try this. https://devdactic.com/user-auth-angularjs-ionic/ this is one of the finest and tested tutorial on the web for token based AUTH.. Maybe that may help you to find the error !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.