<commit_before>/* globals twoFactor */

const state = new ReactiveDict('twoFactor');

state.set('user', '');
state.set('password', '');
state.set('verifying', false);

const getSelector = user => {
  if (typeof user === 'string') {
    if (user.indexOf('@') === -1) {
      return {username: user};
    }
    return {email: user};
  }
  return user;
};

const getAuthCode = (user, password, cb) => {
  const selector = getSelector(user);

  const callback = error => {
    if (error) {
      return typeof cb === 'function' && cb(error);
    }

    state.set('verifying', true);
    state.set('user', user);
    state.set('password', password);

    return typeof cb === 'function' && cb();
  };

  Meteor.call(
    'twoFactor.getAuthenticationCode',
    selector,
    password,
    callback
  );
};

const getNewAuthCode = cb => {
  const selector = getSelector(state.get('user'));
  const password = state.get('password');

  const callback = error => {
    if (error) {
      return typeof cb === 'function' && cb(error);
    }

    return typeof cb === 'function' && cb();
  };

  Meteor.call(
    'twoFactor.getAuthenticationCode',
    selector,
    password,
    callback
  );
};

const verifyAndLogin = (code, cb) => {
  const selector = getSelector(state.get('user'));

  Accounts.callLoginMethod({
    methodName: 'twoFactor.verifyCodeAndLogin',
    methodArguments: [{
      user: selector,
      password: state.get('password'),
      code
    }],
    userCallback: error => {
      if (error) {
        return typeof cb === 'function' && cb(error);
      }

      state.set('verifying', false);
      state.set('user', '');
      state.set('password', '');

      return typeof cb === 'function' && cb();
    }
  });
};

const isVerifying = () => state.get('verifying');

twoFactor.getAuthCode = getAuthCode;
twoFactor.getNewAuthCode = getNewAuthCode;
twoFactor.verifyAndLogin = verifyAndLogin;
twoFactor.isVerifying = isVerifying;<commit_msg>dry up a bit<commit_after>/* globals twoFactor */

const state = new ReactiveDict('twoFactor');

state.set('user', '');
state.set('password', '');
state.set('verifying', false);

const getSelector = user => {
  if (typeof user === 'string') {
    if (user.indexOf('@') === -1) {
      return {username: user};
    }
    return {email: user};
  }
  return user;
};

const callbackHandler = (cb, handlerCb) => {
  return error => {
    if (error) {
      return typeof cb === 'function' && cb(error);
    }

    handlerCb();

    return typeof cb === 'function' && cb();
  };
};

const getAuthCode = (user, password, cb) => {
  const selector = getSelector(user);

  const callback = callbackHandler(cb, () => {
    state.set('verifying', true);
    state.set('user', user);
    state.set('password', password);
  });

  Meteor.call(
    'twoFactor.getAuthenticationCode',
    selector,
    password,
    callback
  );
};

const getNewAuthCode = cb => {
  const selector = getSelector(state.get('user'));
  const password = state.get('password');
  const callback = callbackHandler(cb);

  Meteor.call(
    'twoFactor.getAuthenticationCode',
    selector,
    password,
    callback
  );
};

const verifyAndLogin = (code, cb) => {
  const selector = getSelector(state.get('user'));

  Accounts.callLoginMethod({
    methodName: 'twoFactor.verifyCodeAndLogin',
    methodArguments: [{
      user: selector,
      password: state.get('password'),
      code
    }],
    userCallback: callbackHandler(cb, () => {
      state.set('verifying', false);
      state.set('user', '');
      state.set('password', '');
    })
  });
};

const isVerifying = () => state.get('verifying');

twoFactor.getAuthCode = getAuthCode;
twoFactor.getNewAuthCode = getNewAuthCode;
twoFactor.verifyAndLogin = verifyAndLogin;
twoFactor.isVerifying = isVerifying;
{'fraction_non_alphanumeric': 0.1702296120348377, 'fraction_numerical': 0.0005278437582475588, 'mean_word_length': 3.316628701594533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1611387', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1177, 'n_words': 259}