<commit_before>var q = require('q');

var AuthService = function (provider, storage) {
  if (!provider) { throw new Error('Authentication provider required'); }
  if (!storage) { throw new Error('Storage service required'); }
  var _provider = provider;
  var _storage = storage;
  var _authedUser;

  this.createUser = function (email, password) {
    if (!email) { return q.reject(new Error('Email required')); }
    if (!password) { return q.reject(new Error('Password required')); }
    return _provider.createUser(email, password)
    .then(function (id) {
      var _user = storage.createRecord('user', id);
      return _user.update({});
    });
  };
  this.getID = function () {
    if (_authedUser) { return _authedUser.getID(); }
  };
  this.setRole = function (role) {
    if (!_authedUser) { return q.reject(new Error('Cannot set role before logging in')); }
    var record = storage.createRecord('role', _authedUser.getID());
    return record.update(role);
  };
  this.loginWithEmail = function (email, password) {
    if (!email) { return q.reject(new Error('Email required')); }
    if (!password) { return q.reject(new Error('Password required')); }
    return _provider.loginWithEmail(email, password)
    .then(function (id) {
      _authedUser = storage.createRecord('user', id);
    });
  };
  this.loginWithToken = function (token) {
    if (!token) { return q.reject(new Error('Token required')); }
    return _provider.loginWithToken(token);
  }
  this.isAuthenticated = function () {
    return _provider.isAuthenticated();
  };
  this.logout = function () {
    _authedUser = false;
    return _provider.logout();
  };
  this.requestPasswordReset = function (email) {
    if (!email) { return q.reject(new Error('Email required')); }
    return _provider.requestPasswordReset(email);
  };
  this.resetPassword = function (email, resetToken, newPassword) {
    if (!email) { return q.reject(new Error('Email required')); }
    if (!resetToken) { return q.reject(new Error('Reset token required')); }
    if (!newPassword) { return q.reject(new Error('Password required')); }
    return _provider.resetPassword(email, resetToken, newPassword);
  };
};

module.exports = AuthService;<commit_msg>docs(AuthService): document constructor and methods<commit_after>var q = require('q');
/**
 * A tool for authenticating users
 * @class AuthService
 * @param {AuthProvider} provider the auth provider which this service delegates to
 * @param {StorageService} storage an instance of le-storage-service that is used to create records
 * @returns {service}
 */
var AuthService = function (provider, storage) {
  if (!provider) { throw new Error('Authentication provider required'); }
  if (!storage) { throw new Error('Storage service required'); }
  var _provider = provider;
  var _storage = storage;
  var _authedUser;
  /**
   * Creates a new user
   * @function createUser
   * @memberof AuthService
   * @instance
   * @param {string} email the new user's email
   * @param {string} password the new user's password
   * @returns {promise} resolves with the newly created user record
   */
  this.createUser = function (email, password) {
    if (!email) { return q.reject(new Error('Email required')); }
    if (!password) { return q.reject(new Error('Password required')); }
    var _user;
    return _provider.createUser(email, password)
    .then(function (id) {
      _user = storage.createRecord('user', id);
      return _user.update({});
    })
    .then(function () { return _user });
  };
  /**
   * Logs a user in, given their email and password
   * @function loginWithEmail
   * @memberof AuthService
   * @instance
   * @param {string} email the user's email
   * @param {string} password the user's password
   * @returns {promise} resolves with the user record
   */
  this.loginWithEmail = function (email, password) {
    if (!email) { return q.reject(new Error('Email required')); }
    if (!password) { return q.reject(new Error('Password required')); }
    return _provider.loginWithEmail(email, password)
    .then(function (id) {
      _authedUser = storage.createRecord('user', id);
    })
    .then(function () { return _authedUser });
  };
  /**
   * Logs a user in, given an access token
   * @function loginWithEmail
   * @memberof AuthService
   * @instance
   * @param {string} token the user's access token (such as a secret key)
   * @returns {promise}
   */
  this.loginWithToken = function (token) {
    if (!token) { return q.reject(new Error('Token required')); }
    return _provider.loginWithToken(token);
  }
  /**
   * Checks whether a user is currently authenticated
   * @function isAuthenticated
   * @memberof AuthService
   * @instance
   * @returns {boolean}
   */
  this.isAuthenticated = function () {
    return _provider.isAuthenticated();
  };
  /**
   * Logs a user out
   * @function logout
   * @memberof AuthService
   * @instance
   */
  this.logout = function () {
    _authedUser = false;
    return _provider.logout();
  };
  /**
   * Start the password reset process
   * @function requestPasswordReset
   * @memberof AuthService
   * @instance
   * @param {string} email the user's email address
   * @returns {promise}
   */
  this.requestPasswordReset = function (email) {
    if (!email) { return q.reject(new Error('Email required')); }
    return _provider.requestPasswordReset(email);
  };
  /**
   * Complete the password reset process and change the user's password
   * @function resetPassword
   * @memberof AuthService
   * @instance
   * @param {string} email the user's email address
   * @param {string} resetToken the password reset token given to the user
   * @param {string} newPassword the user's new password
   * @returns {promise}
   */
  this.resetPassword = function (email, resetToken, newPassword) {
    if (!email) { return q.reject(new Error('Email required')); }
    if (!resetToken) { return q.reject(new Error('Reset token required')); }
    if (!newPassword) { return q.reject(new Error('Password required')); }
    return _provider.resetPassword(email, resetToken, newPassword);
  };
};

module.exports = AuthService;
{'fraction_non_alphanumeric': 0.13258317025440314, 'fraction_numerical': 0.0, 'mean_word_length': 3.699616858237548, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4280846', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1766, 'n_words': 530}