<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// a very light wrapper around the real FxaClient to reduce boilerplate code
// and to allow us to develop to features that are not yet present in the real
// client.

'use strict';

define([
  'fxaClient',
  'jquery'
],
function (FxaClient, $) {
  var client;

  function FxaClientWrapper() {
    // nothing to do here.
  }

  FxaClientWrapper.prototype = {
    _withClient: function (callback) {
      var defer = $.Deferred();

      this._getAsync()
        .then(function (client) {
            callback(client, defer);
          }, defer.reject);

      return defer.promise();
    },

    _getAsync: function () {
      var defer = $.Deferred();

      if (client) {
        defer.resolve(client);
      } else {
        $.getJSON('/config', function (data) {
          client = new FxaClient(data.fxaccountUrl);
          defer.resolve(client);
        });
      }

      return defer.promise();
    },

    signIn: function (email, password) {
      return this._withClient(function (client, defer) {
                client.signIn(email, password, { keys: true })
                     .then(defer.resolve, defer.reject);
              });
    },

    signUp: function (email, password) {
      // this jankiness is to appease JSHint
      function signUp(client, defer) {

        function signIn() {
          client.signIn(email, password, { keys: true })
                .then(defer.resolve, defer.reject);
        }

        client.signUp(email, password, { keys: true })
               .then(signIn, defer.reject);

      }
      return this._withClient(signUp);
    },

    verifyCode: function (uid, code) {
      return this._withClient(function (client, defer) {
                client.verifyCode(uid, code)
                       .then(defer.resolve, defer.reject);
              });
    },

    requestPasswordReset: function (email) {
      return this._withClient(function (client, defer) {
                client.passwordForgotSendCode(email)
                      .then(defer.resolve, defer.reject);
              });
    },

    completePasswordReset: function (email, newPassword, token, code) {
      return this._withClient(function (client, defer) {
                client.passwordForgotVerifyCode(code, token)
                      .then(function (result) {
                          client.accountReset(email,
                                     newPassword,
                                     result.accountResetToken)
                          .then(defer.resolve, defer.reject);
                        }, defer.reject);
              });
    }<commit_msg>This is how I though promises should work, everything in a waterfall.<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// a very light wrapper around the real FxaClient to reduce boilerplate code
// and to allow us to develop to features that are not yet present in the real
// client.

'use strict';

define([
  'fxaClient',
  'jquery'
],
function (FxaClient, $) {
  var client;

  function FxaClientWrapper() {
    // nothing to do here.
  }

  FxaClientWrapper.prototype = {
    _withClient: function (callback) {
      return this._getAsync()
        .then(function (client) {
            callback(client);
          });
    },

    _getAsync: function () {
      var defer = $.Deferred();

      if (client) {
        defer.resolve(client);
      } else {
        $.getJSON('/config', function (data) {
          client = new FxaClient(data.fxaccountUrl);
          defer.resolve(client);
        });
      }

      return defer.promise();
    },

    signIn: function (email, password) {
      return this._withClient(function (client) {
                return client.signIn(email, password, { keys: true });
              });
    },

    signUp: function (email, password) {
      return this._withClient(function (client) {
        client.signUp(email, password, { keys: true })
               .then(function () {
                  return client.signIn(email, password, { keys: true });
                });

      });
    },

    verifyCode: function (uid, code) {
      return this._withClient(function (client) {
                return client.verifyCode(uid, code);
              });
    },

    requestPasswordReset: function (email) {
      return this._withClient(function (client) {
                return client.passwordForgotSendCode(email);
              });
    },

    completePasswordReset: function (email, newPassword, token, code) {
      return this._withClient(function (client) {
                return client.passwordForgotVerifyCode(code, token)
                      .then(function (result) {
                          return client.accountReset(email,
                                     newPassword,
                                     result.accountResetToken);
                        });
              });
    }
{'fraction_non_alphanumeric': 0.11125979736188109, 'fraction_numerical': 0.0015293442936341044, 'mean_word_length': 1.8129032258064517, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5276616', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1356, 'n_words': 379}