Search code examples
django-rest-frameworkember-simple-auth

Cannot connect Ember Simple Auth and DRF Token Auth


I have a trouble with Ember Simple Auth.

I'm trying to connect my server-side application, which working on Django 1.9 with DRF, and client-side which working on Ember 2.2.

On server side I'm obtaining token on 'http://localhost:8000/api-token-auth/'. Function requires two args from request: "username" and "password". But Ember Simple Auth send POST request with args: "username[identification]" and "password[password]", and server returns "400". I think that problem with arguments keys.

POST request Responce I tried to change .authenticate method in oauth2-password-grant.js(i can't write custom authenticator because i'm newbee in javascript), but nothing changed.

Manually POST request returns expected answer. Please tell me the way to solve this problem.

And please forgive me for my english.

authenticate(identification, password, scope = []) {
    return new RSVP.Promise((resolve, reject) => {
      const data                = { 'grant_type': 'password', username: identification, password };
      const serverTokenEndpoint = this.get('serverTokenEndpoint');
      const scopesString = Ember.makeArray(scope).join(' ');
      if (!Ember.isEmpty(scopesString)) {
        data.scope = scopesString;
      }
      this.makeRequest(serverTokenEndpoint, data).then((response) => {
        run(() => {
          const expiresAt = this._absolutizeExpirationTime(response['expires_in']);
          this._scheduleAccessTokenRefresh(response['expires_in'], expiresAt, response['refresh_token']);
          if (!isEmpty(expiresAt)) {
            response = Ember.merge(response, { 'expires_at': expiresAt });
          }
          resolve(response);
        });
      }, (xhr) => {
        run(null, reject, xhr.responseJSON || xhr.responseText);
      });
    });
  },

My variant:

const data = { 'grant_type': 'password', 'username': identification, 'password': password };

Solution

  • authenticate: function () {
                // var username = this.getProperties('username');
                // var password = this.getProperties('password');
    
                const {username, password} = this.getProperties('username', 'password');
    
                this.get('session').authenticate('authenticator:oauth2', username, password).catch((reason) => {
                    this.set('errorMessage', reason.error || reason);
                });
            }
    

    It was my mistake.