lynndylanhurley / ng-token-auth

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

login failure error message not propagating from devise_token_auth #205

Open jeffchuber opened 9 years ago

jeffchuber commented 9 years ago

If you try to login, but your account has not been approved, or your email has not been confirmed, it would be good to show the user that error message. However it appears that ng-token-auth treats all failed logins with the same error message. Am I missing something?

It does appear that devise_token_auth is well set up to handle this on the rails end.

I think this is the relevant code from ng-token-auth.js

submitLogin: function(params, opts) {
  if (opts == null) {
    opts = {};
  }
  this.initDfd();
  $http.post(this.apiUrl(opts.config) + this.getConfig(opts.config).emailSignInPath, params).success((function(_this) {
    return function(resp) {
      var authData;
      _this.setConfigName(opts.config);
      authData = _this.getConfig(opts.config).handleLoginResponse(resp, _this);
      _this.handleValidAuth(authData);
      return $rootScope.$broadcast('auth:login-success', _this.user);
    };
  })(this)).error((function(_this) {
    return function(resp) {
      _this.rejectDfd({
        reason: 'unauthorized',
        errors: ['Invalid credentials']
      });
      return $rootScope.$broadcast('auth:login-error', resp);
    };
  })(this));
  return this.dfd.promise;
},
ganySA commented 9 years ago

I have a related challenge. On login failure it ignores the error message sent from server and simply uses the:

reason: unauthorised error[0]: Invalid Credentials

jeffchuber commented 9 years ago

@ganySA - same error! see above why

harakhovich commented 8 years ago

@jeffchuber @ganySA you need to use ng-token-auth event 'auth:login-error'

$scope.$on('auth:login-error', function(ev, reason) { });

and on server you should send error in 'data' key { data: { errors: ['you error'] } } Hope will help somebody.

UPD: success/error on $http is not working. Change to .then(successClb, errorClb) in source code :)

booleanbetrayal commented 8 years ago

see also #219