akf-forum/routes/api/index.js

62 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-03-21 23:53:22 +03:00
const { User, Message, Thread } = require("../../classes");
const db = require("quick.db");
const { Router } = require("express")
const app = Router();
const { request, response } = require("express");
2022-04-03 21:22:06 +03:00
/**
* AUTH TYPE:
headers:
{
username: "Username for client",
password: "Password of selected username for client"
}
*/
/**
* REQUEST TYPE:
* GET /api/action/id
*
* @example message action:
* GET /api/message/0
*
*/
2022-03-21 23:53:22 +03:00
/**
* For intellisense
* @param {request} req
* @param {response} res
*/
2022-04-03 21:22:06 +03:00
app.use((req, res, next) => {
2022-03-21 23:53:22 +03:00
const error = (status, error) =>
res.status(status).json(new ApiResponse(status, { error }))
const { username = null, password = null } = req.headers;
if (!username || !password)
return error(401, "Headers are missing")
const user = db.get("secret." + username);
if (!user)
return error(401, "We have not got any user has got this name")
if (user.key !== password)
return error(401, 'Incorrect Password!')
2022-04-03 21:22:06 +03:00
next();
})
2022-03-21 23:53:22 +03:00
2022-04-03 22:03:47 +03:00
app.use("/messages", require("./routes/message"))
app.use("/users", require("./routes/user"))
app.use("/threads", require("./routes/threads"))
2022-04-03 21:22:06 +03:00
app.all("*", (req, res) => res.status(400).json(new ApiResponse(400, { error: "Bad request" })));
2022-03-21 23:53:22 +03:00
module.exports = app;