lynndylanhurley / ng-token-auth

Token based authentication module for angular.js.
Do What The F*ck You Want To Public License
1.8k stars 234 forks source link

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

Open lentisas opened 9 years ago

lentisas commented 9 years ago

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 commented 9 years ago

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

williamweckl commented 9 years ago

Solved with this: https://github.com/lynndylanhurley/ng-token-auth/issues/36

lentisas commented 9 years ago

Yes ng-resource as well

williamweckl commented 9 years ago

Can you post an example of your service?

lentisas commented 9 years ago

'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 commented 8 years ago

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 commented 8 years ago

@ronniebermejo what was wrong in your rails?

ronniebermejo commented 8 years ago

@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 commented 8 years ago

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