import db from "../../inc/sql.mjs";

const auth = async (req, res, next) => {
  if(!req.session)
    return res.redirect("/login");
  return next();
};

export default (router, tpl) => {
  router.group(/^\/settings/, group => {
    group.get(/$/, auth, async (req, res) => {
      const sessions = await db`
        select *
        from user_sessions
        where user_id = ${ +req.session.id }
        order by last_used desc
      `;

      res.reply({
        body: tpl.render('settings', {
          tmp: null,
          sessions
        }, req)
      });
    });
  });

  return router;
};
