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.

1
  • You are listening to "event:auth-loginRequired", right? Why not do a check there where it comes from and act accordingly? Commented Mar 10, 2016 at 8:39

2 Answers 2

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'
    })
Sign up to request clarification or add additional context in comments.

Comments

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

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.