<commit_before>/**
 * API endpoints for backend
 */

require('dotenv').config();
const config = require('./config');

const router = require('express').Router();

const user = require('./user');

// define api methods here
function apiPostLogin(req, res, db) {
  const pin = parseInt(req.body.pin, 10);
  const pinHashed = user.hash(pin, config.userHashSalt);

  let error = false;

  return db.collection('users')
    .findOne({
      pinHashed
    })
    .then(result => {
      if (!result) {
        // bad login
        res.status(400).json({
          error: true,
          errorMessage: config.msg.errorLoginBad
        });
        return;
      }

      const error = false;
      const apiKey = result.pinHashed;
      const uid = result.uid;
      const name = result.name;

      res.json({
        error,
        api_key: apiKey,
        uid,
        name
      });
    })
    .catch(err => {
      res.status(500).json({
        error: true,
        errorMessage: config.msg.errorServerDb
      });
    });
}

function api(db) {
  router.use((req, res, next) => {
    // redirect requests like ?t=foo/bar to foo/bar
    if (req.query.t) {<commit_msg>403 error on bad login<commit_after>/**
 * API endpoints for backend
 */

require('dotenv').config();
const config = require('./config');

const router = require('express').Router();

const user = require('./user');

// define api methods here
function apiPostLogin(req, res, db) {
  const pin = parseInt(req.body.pin, 10);
  const pinHashed = user.hash(pin, config.userHashSalt);

  let error = false;

  return db.collection('users')
    .findOne({
      pinHashed
    })
    .then(result => {
      if (!result) {
        // bad login
        res.status(403).json({
          error: true,
          errorMessage: config.msg.errorLoginBad
        });
        return;
      }

      const error = false;
      const apiKey = result.pinHashed;
      const uid = result.uid;
      const name = result.name;

      res.json({
        error,
        api_key: apiKey,
        uid,
        name
      });
    })
    .catch(err => {
      res.status(500).json({
        error: true,
        errorMessage: config.msg.errorServerDb
      });
    });
}

function api(db) {
  router.use((req, res, next) => {
    // redirect requests like ?t=foo/bar to foo/bar
    if (req.query.t) {
{'fraction_non_alphanumeric': 0.14634146341463414, 'fraction_numerical': 0.008130081300813009, 'mean_word_length': 2.0885072655217964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '6902981', 'n_tokens_mistral': 747, 'n_tokens_neox': 722, 'n_words': 176}