Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ng-token-auth with ngResource #36

Closed
joelwreed opened this issue Sep 16, 2014 · 10 comments
Closed

Using ng-token-auth with ngResource #36

joelwreed opened this issue Sep 16, 2014 · 10 comments

Comments

@joelwreed
Copy link

I might be doing something wrong but if I use ngResource methods, the token doesn't get passed to the server. If I use plain $http.get it does. Either way generates the same ajax GET call, but whatever ng-token-auth does to hook into $http doesn't seem to extend to ngResource. I tried with both latest angular 1.3rc0 and 1.2.23.

angular.module('spacetoolsApp')
.controller('OrganizationsCtrl', ['$scope', 'Organization', '$http', function ($scope, Organization, $http) {
// THIS NEXT LINE WORKS
$scope.organizations = $http.get('http://www.spacetools.org/api/organizations.json');
// THIS LINE DOESN'T SEND ACCESS TOKEN TO SERVER, THOUGH AJAX CALL IS THE SAME
$scope.organizations = Organization.query();
}]);

@joelwreed
Copy link
Author

So I copied from: "Example configuration when using devise token auth"

angular.module('myApp', ['ng-token-auth'])
.config(function($authProvider) {
$authProvider.configure({
apiUrl: 'http://api.example.com'
});
});

And used my own apiUrl of 'http://www.spacetools.org/api', tried to match the sample code with a full url. However, since ngResource was constructing urls like '/api/organizations.json' the $http wasn't getting the token injected into the request. Perhaps the sample above should just use '/api' ? Or else perhaps explain more? Just a thought. Otherwise, THANK YOU so much for this and the rails gem.

@lynndylanhurley
Copy link
Owner

Thanks, I'll make a note in the readme.

lynndylanhurley added a commit that referenced this issue Sep 16, 2014
@williamweckl
Copy link

Helped me too man! Thank you.

@piyushchauhan2011
Copy link

Cool 👍

@Zhong-z
Copy link

Zhong-z commented Jun 30, 2015

👍

@grantgeorge
Copy link

Hey yall - I just stumbled upon this when I was having an issue using ng-resource.

The path you pass in as a param to $resource has to explicitly match that of the apiUrl you provide for $authProvider. Example (didn't work for $resource, worked for $http):

$authProvider.configure({
    apiUrl: "api/v1"
});
.factory('Articles', function ($resource) {
      return $resource('/api/v1/articles/:articleId', {
        articleId: '@id'
      }
      });
    });

It was not working because I did not include the forward slash in the beginning of the config. Fixed by changing the config to:

$authProvider.configure({
    apiUrl: "/api/v1"
});

Hope this may help someone stuck with this.

@piyushchauhan2011
Copy link

Yeah, we had the same problems and putting slash '/' in front of the url solved the issue. @grantgeorge 👍

@gowthamgts
Copy link

gowthamgts commented Oct 8, 2016

@grantgeorge So, how should I provide the apiUrl if the API is running in an address like http://localhost:3000? I tried the following:

todoApp.config(function($authProvider){
  $authProvider.configure({
      apiUrl: 'http://localhost:3000'
  });
});

And still the headers are not being injected into the requests.

@grantgeorge
Copy link

@gowthamgts try using a relative URL, ex '/' if that's the path to the appropriate resource (see my example). Is that your question?

@gowthamgts
Copy link

@grantgeorge No pal, I have to use a fully qualified name. The headers are not injected by default, but it is working fine now after I changed the storage option to localStorage. I guess the problem is with ipcookies may be. Thanks anyway...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants