<commit_before>const router = require('koa-router')()
const jwt = require('koa-jwt')

const log = require('../lib/log')
const Page = require('../models/page')
const User = require('../models/user')

router.post('/token-auth', function *(next) {
  const body = this.request.body

  if (!body.username || !body.password) {
    this.status = 400
    this.body = {
      error: 'User and/or password empty'
    }
    return
  }

  const query = User.where({ _id: body.username })

  try {
    var user = yield query.findOne()
  } catch (err) {
    log('Couldn\'t load user', err)
  }

  if (!user) {
    this.status = 400
    this.body = {
      error: 'User doesn\'t exist'
    }
    return
  }

  // Compare password with the one within the DB
  const isMatch = user.tryPassword(body.password)

  if (isMatch) {
    const token = jwt.sign(body, process.env.SESSION_SECRET, {
      expiresIn: 300
    })

    this.body = {
      token
    }

    return
  }

  this.status = 400

  this.body = {
    error: 'Wrong password'
  }

  yield next
})

router.post('/token-refresh', function *(next) {
  const token = this.request.body.token

  try {
    const decoded = jwt.verify(token, process.env.SESSION_SECRET)
  } catch (err) {
    this.status = 401
    this.body = { error: err }

    return
  }

  const query = User.where({ _id: decoded.username })

  try {
    var user = yield query.findOne()
  } catch (err) {
    log('Couldn\'t load user', err)
  }

  if (!user) {
    this.status = 401
    this.body = {
      error: 'User doesn\'t exist'
    }

    return
  }

  const isMatch = user.tryPassword(decoded.password)

  if (isMatch) {
    this.body = {
      token: jwt.sign(decoded, process.env.SESSION_SECRET, {
        expiresIn: 300
      })
    }

    return
  }

  this.status = 401

  this.body = {
    error: 'Wrong password'
  }

  console.log(decoded)
})

router.get('/pages', function *(next) {
  try {
    var pages = yield Page.find()
  } catch (err) {
    return log('Couldn\'t load pages', err)
  }

  var data = []

  for (var page in pages) {
    var attributes = pages[page].toObject()
    var id = attributes.id

    delete attributes.id
    delete attributes._id
    delete attributes.__v

    data.push({
      id,
      type: 'pages',
      attributes
    })
  }

  this.body = { data }
  yield next
})

router.get('/pages/:id', function *(next) {
  const query = Page.where({ _id: this.params.id })

  try {
    var page = yield query.findOne()
  } catch (err) {
    return log('Couldn\'t load page', err)
  }

  var attributes = page.toObject()
  var id = attributes.id

  delete attributes.id
  delete attributes._id
  delete attributes.__v

  this.body = {
    data: {
      id,
      type: 'page',
      attributes
    }
  }

  yield next
})

router.get('/users', function *(next) {
  try {
    var users = yield User.find()
  } catch (err) {
    return log('Couldn\'t load users', err)
  }

  var data = []

  for (var user in users) {
    const details = users[user].toObject()

    data.push({
      id: details._id,
      type: 'users',
      attributes: {
        email: details.email
      }
    })
  }

  this.body = { data }
  yield next
})

module.exports = router
<commit_msg>Load a single user if wished<commit_after>const router = require('koa-router')()
const jwt = require('koa-jwt')

const log = require('../lib/log')
const Page = require('../models/page')
const User = require('../models/user')

router.post('/token-auth', function *(next) {
  const body = this.request.body

  if (!body.username || !body.password) {
    this.status = 400
    this.body = {
      error: 'User and/or password empty'
    }
    return
  }

  const query = User.where({ _id: body.username })

  try {
    var user = yield query.findOne()
  } catch (err) {
    log('Couldn\'t load user', err)
  }

  if (!user) {
    this.status = 400
    this.body = {
      error: 'User doesn\'t exist'
    }
    return
  }

  // Compare password with the one within the DB
  const isMatch = user.tryPassword(body.password)

  if (isMatch) {
    const token = jwt.sign(body, process.env.SESSION_SECRET, {
      expiresIn: 300
    })

    this.body = {
      token
    }

    return
  }

  this.status = 400

  this.body = {
    error: 'Wrong password'
  }

  yield next
})

router.post('/token-refresh', function *(next) {
  const token = this.request.body.token

  try {
    const decoded = jwt.verify(token, process.env.SESSION_SECRET)
  } catch (err) {
    this.status = 401
    this.body = { error: err }

    return
  }

  const query = User.where({ _id: decoded.username })

  try {
    var user = yield query.findOne()
  } catch (err) {
    log('Couldn\'t load user', err)
  }

  if (!user) {
    this.status = 401
    this.body = {
      error: 'User doesn\'t exist'
    }

    return
  }

  const isMatch = user.tryPassword(decoded.password)

  if (isMatch) {
    this.body = {
      token: jwt.sign(decoded, process.env.SESSION_SECRET, {
        expiresIn: 300
      })
    }

    return
  }

  this.status = 401

  this.body = {
    error: 'Wrong password'
  }

  console.log(decoded)
})

router.get('/pages', function *(next) {
  try {
    var pages = yield Page.find()
  } catch (err) {
    return log('Couldn\'t load pages', err)
  }

  var data = []

  for (var page in pages) {
    var attributes = pages[page].toObject()
    var id = attributes.id

    delete attributes.id
    delete attributes._id
    delete attributes.__v

    data.push({
      id,
      type: 'pages',
      attributes
    })
  }

  this.body = { data }
  yield next
})

router.get('/pages/:id', function *(next) {
  const query = Page.where({ _id: this.params.id })

  try {
    var page = yield query.findOne()
  } catch (err) {
    return log('Couldn\'t load page', err)
  }

  var attributes = page.toObject()
  var id = attributes.id

  delete attributes.id
  delete attributes._id
  delete attributes.__v

  this.body = {
    data: {
      id,
      type: 'page',
      attributes
    }
  }

  yield next
})

router.get('/users', function *(next) {
  try {
    var users = yield User.find()
  } catch (err) {
    return log('Couldn\'t load users', err)
  }

  var data = []

  for (var user in users) {
    const details = users[user].toObject()

    data.push({
      id: details._id,
      type: 'users',
      attributes: {
        email: details.email
      }
    })
  }

  this.body = { data }
  yield next
})

router.get('/users/:id', function *(next) {
  const query = User.where({ _id: this.params.id })

  try {
    var user = yield query.findOne()
  } catch (err) {
    return log('Couldn\'t load user', err)
  }

  var attributes = user.toObject()
  var id = attributes._id

  delete attributes._id
  delete attributes.__v
  delete attributes.password

  this.body = {
    data: {
      id,
      type: 'user',
      attributes
    }
  }

  yield next
})

module.exports = router

{'fraction_non_alphanumeric': 0.1413863109048724, 'fraction_numerical': 0.0069605568445475635, 'mean_word_length': 2.262535477767266, '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': '2169149', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2323, 'n_words': 595}