Angularjs - authorization header not added in Safari

I am currently creating a webapp using the Django + Django Rest Framework (DRF) at the back and Angularjs to create a user interface.

All requests are authenticated using basic token authentication, which is built into DRF. I applied a basic interceptor in Angular to add a token, which is stored in the session store after login, for each request. This works fine in Google Chrome (I see that the authorization header is actually added to each request with the inspector) and Firefox. However, in Safari, the header is not added to some queries.

First of all, this is the interceptor that I use:

app.factory('authInterceptor', ['$rootScope', '$q', '$window', function($rootScope, $q, $window){
    return {
        // This method adds the authentication token to each requests' header
        request: function(config){
            config.headers = config.headers || {};

            // add authentication token in header
            try {
                var token = $window.sessionStorage.token;
            } catch(err){
                token = undefined;
            }
            if(token !== undefined){
                config.headers.Authorization = 'Token ' + token;
            }
            return config;
        },
        response: function(response){
            if(response.status === 401){
                // user not authenticated
            }
            return response || $q.when(response);
        }
    };
}]);

One specific resource that gives me a headache in Safari:

app.factory('Coach', ['$resource', '$rootScope',
    function($resource, $rootScope){
        return $resource(
            $rootScope.api_url + 'api/coaches/:coachId/?format=json',
            {
                coachId: '@id'
            }
            ,
            {
                getByUserId: {method:'GET', url: $rootScope.api_url + 'api/coaches', isArray:false},
                update: {method:'PUT'}
            }
        );
}]);

withCredentials getByUserId, .

$httpService. , Safari sessionStorage. . .

-?

+4
1

. , , Angularjs URL- Django .

Angular , 301 ( ) . Django Rest Framework URL- ( -). - IE Safari . .

, .

+13

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


All Articles