homepage.js/routes/admin.js

46 lines
1.0 KiB
JavaScript
Raw Normal View History

2023-02-25 07:32:11 +01:00
const handler = require('express-async-handler')
const Helpers = require('../helpers');
const db = require('../models');
async function login(req, res) {
res.send(await Helpers.ViewLoader.load('admin/login.pug', {
current_route: req.originalUrl
}));
}
async function apiLogin(req, res) {
if (req.session.user) {
res.send('Already logged in');
return;
}
const user = (await db.User.authenticate(req.body));
if (!user) {
res.status(401).send('Bad auth');
}
const session = await user.createSession();
req.session.user = session;
2023-02-27 08:24:49 +01:00
res.redirect('/admin/panel');
return;
}
async function panel(req, res) {
res.send(await Helpers.ViewLoader.load('admin/panel.pug', {
current_route: req.originalUrl
}));
2023-02-25 07:32:11 +01:00
return;
}
module.exports = (router) => {
2023-02-27 08:24:49 +01:00
// login
2023-02-25 07:32:11 +01:00
router.get('/login', handler(login));
router.get('/admin/login', handler(login));
router.post('/admin/login', handler(apiLogin));
2023-02-27 08:24:49 +01:00
// panel
router.get('/admin/panel', handler(panel));
2023-02-25 07:32:11 +01:00
}