1

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.

Mavlarn
  • 3,807
  • 2
  • 37
  • 57
  • You are listening to "event:auth-loginRequired", right? Why not do a check there where it comes from and act accordingly? – George Kagan Mar 10 '16 at 08:39

2 Answers2

1

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'
    })
Mavlarn
  • 3,807
  • 2
  • 37
  • 57
0

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

Community
  • 1
  • 1
Devendiran Kumar
  • 223
  • 3
  • 12