<commit_before>`use strict`

const Auth = require('../models/auth')
const userGame = require('../models/userGame')
const localAuth = require('../auth/local')

module.exports.getUserGames = ({query: {userId}}, res, next) => {
  Auth.forge({id: userId})
  .fetch({withRelated: ['games'], require: true})
  .then((usergames) => {
    res.status(200).json(usergames)
  })
  .catch((err) => {
    next(err)
  })
}

module.exports.register = (req, res, next) => {
  Auth.createUser(req)
  .then((user) => { return localAuth.encodeToken(user[0]) })
  .then((token) => {
    res.status(200).json({
      status: 'success',
      token: token
    })
  })
  .catch((err) => {
    res.status(500).json({
      status: 'error'
    })
  })
}

module.exports.login = (req, res, next) => {
  const username = req.body.username
  const password = req.body.password
  Auth.getUser(username)
  .then((response) => {
    Auth.comparePass(password, response.password)
    return response
  })<commit_msg>changed getUserGames so it gets the logged in user's games but not someone elses<commit_after>`use strict`

const Auth = require('../models/auth')
const userGame = require('../models/userGame')
const localAuth = require('../auth/local')
const {knex} = require('../db/database')

module.exports.getUserGames = (req, res, next) => {
  let header = req.headers.authorization.split(' ')
  let token = header[1]
  localAuth.decodeToken(token, (err, payload) => {
    return knex('users').where({id: parseInt(payload.sub)}).first()
    .then((user) => {
      const id = user.id
      Auth.forge({id})
      .fetch({withRelated: ['games'], require: true})
      .then((usergames) => {
        res.status(200).json(usergames)
      })
      .catch((err) => {
        next(err)
      })
    })
  })
}

module.exports.register = (req, res, next) => {
  Auth.createUser(req)
  .then((user) => { return localAuth.encodeToken(user[0]) })
  .then((token) => {
    res.status(200).json({
      status: 'success',
      token: token
    })
  })
  .catch((err) => {
    res.status(500).json({
      status: 'error'
    })
  })
}

module.exports.login = (req, res, next) => {
  const username = req.body.username
  const password = req.body.password
  Auth.getUser(username)
  .then((response) => {
    Auth.comparePass(password, response.password)
    return response
  })
{'fraction_non_alphanumeric': 0.19017094017094016, 'fraction_numerical': 0.008974358974358974, 'mean_word_length': 3.1729055258467023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '4002261', 'n_tokens_mistral': 808, 'n_tokens_neox': 789, 'n_words': 150}