import request from 'superagent';
import { Promise } from 'bluebird';
import { isValidToken } from './jwt';

export default class TokenClient {
  constructor(config) {
    this.user = config.user;
    this.password = config.password;
    this.routes = config.routes;
    this.token = config.token;
  }

  getValidToken() {
    return new Promise(this.tokenPromiseHandler.bind(this));
  }

  tokenPromiseHandler(resolve, reject) {
    if (isValidToken(this.token)) {
      resolve(this.token);
    } else {
      this.requestToken(this.user, this.password, function(err, token) {
        if (err) {
          reject(err);
        } else {
          this.token = token;
          resolve(this.token);
        }
      }.bind(this));
    }
  }

  requestToken(user, pass, callback) {
    request.post(this.routes.token)
      .send({
        'user' : user,
        'password' : pass
      })
      .end(function(err, result) {
        if (err) {
          callback(err);
        } else if (!isValidToken(result.body.token)) {
          if (result.serverError) {
            callback('Server error: '+ result.status );
          } else {
            callback('Server returned invalid JWT token');
          }
        } else {
          callback(null, result.body.token);
        }
      });
  }
}

@@ -549,24 +549,16 @@
 ssword, 
-function
 (err, to
@@ -561,16 +561,19 @@
 , token)
+ =%3E
  %7B%0A     
@@ -704,27 +704,16 @@
 %0A      %7D
-.bind(this)
 );%0A    %7D

Replace bind with fat arrow syntax