2025-02-25 16:35:22 -05:00
|
|
|
import { Router } from "express";
|
2025-03-02 04:09:17 -05:00
|
|
|
import { initDb } from "../db.js";
|
|
|
|
|
|
|
|
let db = await initDb();
|
2025-02-25 16:35:22 -05:00
|
|
|
const router = Router();
|
|
|
|
|
2025-02-25 19:51:46 -05:00
|
|
|
//todo: fix jank
|
|
|
|
|
2025-02-25 16:35:22 -05:00
|
|
|
router.get('/login', (req, res, next) => {
|
|
|
|
res.ctx.mainPage = 'form'
|
|
|
|
res.ctx.mainCtx = {
|
|
|
|
title: "Log in",
|
2025-02-25 19:47:28 -05:00
|
|
|
message: 'Log into an existing account.',
|
|
|
|
action: "/api/form/you/login",
|
2025-02-25 16:35:22 -05:00
|
|
|
inputs: [
|
|
|
|
{
|
|
|
|
"key": "Username",
|
|
|
|
"type": "text",
|
|
|
|
"name": "user",
|
|
|
|
"default": ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "Password",
|
|
|
|
"type": "password",
|
|
|
|
"name": "pass",
|
|
|
|
"default": ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
|
2025-02-25 19:51:46 -05:00
|
|
|
router.get('/logout', (req, res, next) => {
|
|
|
|
res.ctx.mainPage = 'form'
|
|
|
|
res.ctx.mainCtx = {
|
|
|
|
title: "Log out",
|
|
|
|
message: 'Are you sure?',
|
|
|
|
action: "/api/form/you/logout",
|
|
|
|
inputs: []
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
|
2025-02-25 16:35:22 -05:00
|
|
|
router.get('/new', (req, res, next) => {
|
|
|
|
res.ctx.mainPage = 'form'
|
|
|
|
res.ctx.mainCtx = {
|
|
|
|
title: "Register",
|
2025-02-25 19:47:28 -05:00
|
|
|
message: 'Create an account and join the community!',
|
|
|
|
action: "/api/form/you/new",
|
2025-02-25 16:35:22 -05:00
|
|
|
inputs: [
|
|
|
|
{
|
|
|
|
"key": "Username",
|
|
|
|
"type": "text",
|
|
|
|
"name": "user",
|
|
|
|
"default": ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "Password",
|
|
|
|
"type": "password",
|
|
|
|
"name": "pass",
|
|
|
|
"default": ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "Password (again)",
|
|
|
|
"type": "password",
|
|
|
|
"name": "pass2",
|
|
|
|
"default": ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
|
2025-03-02 04:09:17 -05:00
|
|
|
router.get('/messages', async (req, res, next) => {
|
|
|
|
let {username, valid} = res.auth;
|
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let messages = await db.all('SELECT * FROM messages WHERE username = ? ORDER BY date DESC LIMIT ? OFFSET ?', [
|
|
|
|
username,
|
|
|
|
10,
|
|
|
|
0
|
|
|
|
]);
|
|
|
|
|
|
|
|
await db.run('UPDATE messages SET status = ? WHERE username = ?', [
|
|
|
|
'read',
|
|
|
|
username
|
|
|
|
]);
|
|
|
|
|
|
|
|
res.ctx.mainPage = 'messages'
|
|
|
|
res.ctx.mainCtx = {
|
|
|
|
messages
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
|
2025-02-25 16:35:22 -05:00
|
|
|
export default router;
|