const { loginUser } = require("../services/auth.service");
const db = require("../db");

const LOCK_TIME_MINUTES = 5;

async function postLogin(req, res) {
  const { username, password } = req.body;

  try {
    const user = await loginUser(
      db,
      username,
      password,
      LOCK_TIME_MINUTES
    );

    req.session.user = user;
    res.redirect("/dashboard");

  } catch (error) {
    res.render("login", { error });
  }
}

function getLogin(req, res) {
  res.render("login", { error: null });
}

module.exports = {
  getLogin,
  postLogin
};
