<commit_before>import crypto from 'crypto';
import config from '../config';

export default class {

  constructor(auth) {
    this.auth = auth;
  }

  /*
  * First checks if user exists, if so -> login
  * If user doesn't exist -> try to create user
  */
  process(httpRequest, httpResponse) {
    let userInfo = {
      username: httpRequest.body.name,
      password: httpRequest.body.password,
      email: httpRequest.body.email,
      type: httpRequest.body.type
    };

    try {
      if (this.loginUser(userInfo)) {
        let token = this.generateToken(userInfo);
        httpResponse.status(201);
        httpResponse.send({
          token: token
        });
        return;
      }
    } catch (err) {}

    if (config.auth.register === false) {
      httpResponse.send("401, Could not create user");
    } else {
      try {
        if (this.createUser(userInfo)) {
          let token = this.generateToken(userInfo);
          httpResponse.status(201);
          httpResponse.send({
            token: token
          });
          return;
        }
      } catch (err) {
        // Cannot create user
        httpResponse.send("400, Could not create user");
        return;
      }

      httpResponse.send("404, Could not login or register user");
    }
  }

  createUser(userInfo) {
    let userCreated = this.auth.userAdd(userInfo.username, userInfo.password, userInfo.email);
    if (userCreated) {
      return true;
    } else {
      throw new Error("Could not create user");
    }
  }

  loginUser(userInfo) {
    try {
      return this.auth.userLogin(userInfo.username, userInfo.password, userInfo.email);
    } catch (err) {
      throw new Error("Could not login user");
    }
  }

  generateToken(userInfo) {
    let token = crypto.randomBytes(64).toString('hex');
    this.auth.addTokenToDB(userInfo.username, token);
    return token;
  }

}<commit_msg>Move to using promises in auth-user-login<commit_after>import crypto from 'crypto';
import config from '../config';

export default class {

  constructor(auth) {
    this.auth = auth;
  }

  /*
  * First checks if user exists, if so -> login
  * If user doesn't exist -> try to create user
  */
  process(httpRequest, httpResponse) {
    let userInfo = {
      username: httpRequest.body.name,
      password: <PASSWORD>,
      email: httpRequest.body.email,
      type: httpRequest.body.type
    };
    let userLoggedIn = false;
    let request;

    return new Promise((resolve, reject) => {
      request = this.loginUser(userInfo);

      request.then((result) => {
        if (result === true) {

          userLoggedIn = true;
          let token = this.generateToken(userInfo);
          httpResponse.status(201);
          httpResponse.send({
            token: token
          });

        }
        else {
          request = this.createUser(userInfo);
          request
            .then((result) => {

            if (result === true) {
              userLoggedIn = true;
              let token = this.generateToken(userInfo);
              httpResponse.status(201);
              httpResponse.send({
                token: token
              });
            }

          });
        }
      }).then(() => {
        resolve(userLoggedIn);
      });
    }).catch((error) => {
      httpResponse.send("404, user not found");
    });
  }

  createUser(userInfo) {
    return this.auth.userAdd(userInfo.username, userInfo.password, userInfo.email);
  }

  loginUser(userInfo) {
    return this.auth.userLogin(userInfo.username, userInfo.password, userInfo.email);
  }

  generateToken(userInfo) {
    let token = crypto.randomBytes(64).toString('hex');
    this.auth.addTokenToDB(userInfo.username, token);
    return token;
  }

}
{'fraction_non_alphanumeric': 0.12288704051515964, 'fraction_numerical': 0.007512744834987926, 'mean_word_length': 2.124895222129086, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 13, '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': '2109502', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1069, 'n_words': 246}