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

Keep getting {"errors":["Authorized users only."]}. Auth headers not set #146

Open
lentisas opened this issue May 6, 2015 · 9 comments
Open

Comments

@lentisas
Copy link

lentisas commented May 6, 2015

I can register and log in successfully but after that all requests to the server for does not include these headers access-token, expiry, Referer, token-type, uid and hence i keep getting {"errors":["Authorized users only."]}.
When i do a validateUser it sends the headers and validates successfully. Is there anything else i am missing?
This is my .config
.config(function($authProvider) {
$authProvider.configure({
apiUrl: '/api'
});
})

@williamweckl
Copy link

same problem here, are you using ng-resource too?

@williamweckl
Copy link

Solved with this: #36

@lentisas
Copy link
Author

Yes ng-resource as well

@williamweckl
Copy link

Can you post an example of your service?

@lentisas
Copy link
Author

'use strict';

angular.module('app').service('Adapter', ['$q','$resource', function Adapter($q,$resource) {

var resMgr = {
models: {}
};

resMgr.getName = function(model) {
  model = model.replace(/-/gi, '_');
  model = model.underscore().split('_');
  model = model.slice(0, model.length - 1).join('_').camelize() + model[model.length - 1].classify();
  return model;
};

resMgr.getUrl = function(model) {
  model = model.underscore().split('_');
  model = model.slice(0, model.length - 1).join('_').camelize() + model[model.length - 1].pluralize().camelize();
  return model.underscore();
};

resMgr.getModel = function (model, config) {
  config = config || {};
  config.name = model = this.getName(model);

  if(!config.url) config.url           = this.getUrl(model);
  if(!config.key) config.key           = model.underscore();
  if(!config.display)  config.display  = model.underscore().titleize();
  if(!config.displays) config.displays = config.url.titleize();

  return config;
};

resMgr.register = function (model, apiUrl, params, config) {
  model = this.getName(model);
  params = params || {id: '@id'};
  config = config || {};

  if(!this.models[model]) {
    var klass = $resource(apiUrl, params, {
      query:  { method: 'GET', isArray: true },
      update: { method: 'PUT' }
    });
    app.factory(model, function () {
      return klass;
    });
    this.models[model] = this.getModel(model, config);
    this.models[model]['apiUrl'] = apiUrl;
    this.models[model]['klass'] = klass;
  }
  return this.models[model];
};

resMgr.query = function (model, data, callback) {
  var d = $q.defer(); model = this.getName(model);
  this.models[model]['klass'].query(data, function (response, headers) {
    if(response.error || response.errors) {
      d.reject(response);
    } else {
      d.resolve(response);
    }
    callback && callback(response, headers('_meta_total'));
  }, function (response) { console.log(response) });
  return d.promise;
};

resMgr.get = function (model, data, callback) {
  var d = $q.defer(); model = this.getName(model);
  this.models[model]['klass'].get(data, function (response, headers) {
    response.error? d.reject(response) : d.resolve(response);
    callback && callback(response);
  }, function (response) { console.log(response) });
  return d.promise;
};

resMgr.create = function (model, data, callback) {
  var d = $q.defer(); model = this.getName(model);
  new this.models[model]['klass'](data).$save(function (response) {
    response.error? d.reject(response) : d.resolve(response);
    callback && callback(response);
  }, function (response) { console.log(response) });
  return d.promise;
};

resMgr.update = function (model, data, callback) {
  var d = $q.defer(); model = this.getName(model);

  new this.models[model]['klass'](data).$update(function (response) {
    response.error? d.reject(response) : d.resolve(response);
    callback && callback(response);
  }, function (response) { console.log(response) });

  return d.promise;
};

resMgr.delete = function (model, data, callback) {
  var d = $q.defer(); model = this.getName(model);

  new this.models[model]['klass'](data).$delete(function (response) {
    response.error? d.reject(response) : d.resolve(response);
    callback && callback(response);
  }, function (response) { console.log(response) });

  return d.promise;
};

window.Adapter = resMgr;
return resMgr;

}]);

I noticed i get the problem when i use my resMgr.register(). But even after careful observation there doesn't seem to be anything wrong with the function

@ronniebermejo
Copy link

I had the same error using Rails as backend, after days researching I found the problem resided on the rails side(nothing to do with Authorization). The error message was a bit misleading. Check your backend related logic. My 2 cents.

@IamNaN
Copy link

IamNaN commented Oct 16, 2015

@ronniebermejo what was wrong in your rails?

@ronniebermejo
Copy link

@IamNaN i had a method named ''transaction' which was overwriting a super method.
So my learning was the 'auth error' could mean 'a generic error' on the rails side.

@fabiancarlos
Copy link

Another possible fact: When you use cancan (or cancancan actually) you need to define correctly the permissions, locally sometimes that just ignored, but in the server (heroku for example) they can piss you up. @IamNaN @ronniebermejo

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

5 participants