var global = (typeof window !== 'undefined') ? window : {},
    Ember = global.Ember;

/**
  Authenticator for use with Devise.

  This authenticator supports Devise's rememberable module.

  @class Devise
  @namespace Authenticators
  @extends Base
*/

var Devise = Ember.SimpleAuth.Authenticators.Base.extend({
  /**
    The endpoint on the server the authenticator acquires the access token
    from.

    @property serverTokenEndpoint
    @type String
    @default '/token'
  */
  serverTokenEndpoint: '/users/sign_in',

  /**
    Restores the session from a set of session properties; __will return a
    resolving promise when there's a non-empty `auth_token` in the
    `properties`__ and a rejecting promise otherwise.

    @method restore
    @param {Object} properties The properties to restore the session from
    @return {Ember.RSVP.Promise} A promise that when it resolves results in the session being authenticated
  */
  restore: function(properties) {
    var _this = this;
    return new Ember.RSVP.Promise(function(resolve, reject) {
      var data = {
        auth_token: properties.auth_token
      };
      return _this.makeRequest(data, resolve, reject);
    });
  },

  /**
    Authenticates the session with the specified `credentials`; the credentials
    are `POST`ed to the `serverTokenEndpoint` and if they are valid the server
    returns an access token in response . __If the credentials are
    valid and authentication succeeds, a promise that resolves with the
    server's response is returned__, otherwise a promise that rejects with the
    error is returned.

    @method authenticate
    @param {Object} options The credentials to authenticate the session with
    @return {Ember.RSVP.Promise} A promise that resolves when an access token is successfully acquired from the server and rejects otherwise
  */
  authenticate: function(credentials) {
    var _this = this;
    return new Ember.RSVP.Promise(function(resolve, reject) {
      var data = {
        email: credentials.identification,
        password: credentials.password
      };
      return _this.makeRequest(data, resolve, reject);
    });
  },

  /**
    Cancels any outstanding automatic token refreshes.

    @method invalidate
    @return {Ember.RSVP.Promise} A resolving promise
  */
  invalidate: function() {
    Ember.run.cancel(this._refreshTokenTimeout);
    delete this._refreshTokenTimeout;
    return new Ember.RSVP.Promise(function(resolve) {
      return resolve();
    });
  },

  /**
    Sends an `AJAX` request to the `serverTokenEndpoint`. This will always be a
    _"POST_" request with content type _"application/x-www-form-urlencoded".

    This method is not meant to be used directly but serves as an extension
    point to e.g. add _"Client Credentials".

    @method makeRequest
    @param {Object} data The data to send with the request, e.g. email and password or the auth_token
    @return {Ember.RSVP.Promise} A promise that resolves when a auth_token is successfully acquired from the server and rejects otherwise
    @protected
  */
  makeRequest: function(data, resolve, reject) {
    return Ember.$.ajax({
      url: this.serverTokenEndpoint,
      type: "POST",
      data: data,
      dataType: "json",
      contentType: "application/x-www-form-urlencoded"
    }).then((function(response) {
      return Ember.run(function() {
        return resolve(Ember.$.extend(response, {
          auth_token: response.auth_token
        }));
      });
    }), function(xhr, status, error) {
      return Ember.run(function() {
        return reject(xhr.responseText);
      });
    });
  }
});

export { Devise };

@@ -375,21 +375,19 @@
 es the a
-ccess
+uth
  token%0A 
@@ -462,20 +462,28 @@
 fault '/
-toke
+users/sign_i
 n'%0A  */%0A
@@ -1368,29 +1368,27 @@
 returns an a
-ccess
+uth
  token in re
@@ -1764,21 +1764,19 @@
 hen an a
-ccess
+uth
  token i

update comments in the devise authenticator