add config in angular resource

831 Views Asked by At

I am using http-auth-interceptor for authentication. In http-auth-interceptor, I use the following way to login:

        var data = 'username=' + encodeURIComponent(user.userId) + '&password=' + encodeURIComponent(user.password);
        $http.post('api/authenticate', data, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            ignoreAuthModule: 'ignoreAuthModule'
        })

ignoreAuthModule is used to tell ignoreAuthModule that this login method will be ignored by the auth interceptor.

Now, I have some request with $resource, like:

.factory('SomeDataService', function ($resource) {
    return $resource('api/some/data', {}, {
       'get': { method: 'GET'}
    });
})

I want SomeDataService.get() is also ignored by the auth interceptors, because I need to control the 401 error by myself.

So, my question is, is there any way for ngResource that I can set config like that in $http.

[update based on comment] I have listened the login-required event:

    $rootScope.$on('event:auth-loginRequired', function (rejection) {
        $log.log(rejection);
        // I need to get the request url and for some specific url, need to do something.
        $rootScope.loginPopup();
    });

But the 'rejection' parameter has no context data of request I need. I need to get the request url and check, for some specified url, I need to do something.

2

There are 2 best solutions below

0
On

ngResource module is build on top of $http.Hence it is not possible to configure all the stuffs you can do with $http in $resource.I think the below link will be guide you to have a clear understanding on $http and $resource

0
On

After checking the document of ngResource, I got the solution as below:

.factory('SomeDataService', function ($resource) {
    return $resource('api/some/data', {}, {
       'get': { method: 'GET', ignoreAuthModule: 'ignoreAuthModule'}
    });
})

Just add the config item as above. It will be equivalent ad:

$http.post('api/some/data', data, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        ignoreAuthModule: 'ignoreAuthModule'
    })