bigly-chat/form/auth.js
2024-11-04 04:21:47 -05:00

46 lines
No EOL
1.1 KiB
JavaScript

import Route from "../route.js";
import initDb from "../db.js";
let db = await initDb();
let main = new Route([], async function (req, res, input) {
let { route } = req.params;
let { id } = req.query;
let body = { ...req.cookies, ...req.body };
let { token } = body;
let match = await db.all('SELECT * FROM token WHERE token = ?', [
token || 'blah'
]);
let username = match[0] ? match[0].username : '!nobody';
let valid = await db.all('SELECT * FROM auth WHERE username = ?', [
username
]);
let isRead = false;
if (username) {
let msgs = await db.all('SELECT * FROM message WHERE username = ? AND read = ?', [
username,
'false'
]);
isRead = msgs.length > 0
}
return {
username,
valid: valid[0] ? valid[0].valid : 'noexist',
url: `${process.env.URL}/client/${route}?id=${id || ''}`,
icon: `${process.env.URL}/static/img/logo.png`,
rootUrl: process.env.URL,
ogType: 'website',
isRead: isRead ? 'unread' : 'read'
};
});
export default main;