apimiddleware done?

Not fully tested
This commit is contained in:
newtbot
2024-01-16 20:54:18 +08:00
parent 7e4b2d8026
commit 915e4a7e5c
7 changed files with 139 additions and 132 deletions

View File

@ -1,6 +1,5 @@
const { getAPIKey , addAPIKey } = require("../functions/apiDatabase.js");
const { hashAPIKey } = require("../functions/bcrypt.js");
const { generateUUID } = require("../functions/generateUUID.js");
const express = require("express");
const router = express.Router();
@ -17,21 +16,18 @@ router.get("/", async (req, res, next) => {
/*
1) ensure user is logged in (frontend session validation blah or wtv)
2) when user click on generate api key button, it will generate a random api key
2) when user click on generate api key button, it will generate a random api key. how to get userid can be done by session or wtv
3) hash the api key
4) store the api key in database
*/
router.post("/new", async (req, res, next) => {
try {
let uuid = await generateUUID()
//attach uuid to req.body
req.body.apikey = uuid
//hash apikey
req.body.apikey = await hashAPIKey(req.body.apikey)
await addAPIKey(req.body);
res.sendStatus(200);
//curl localhost/api/v0/apikey/new -H "Content-Type: application/json" -X POST -d
//'{"userid": 1, "permission": "canWrite"}'
const apikey = await addAPIKey(req.body.userid, req.body.permission);
//console.log(typeof req.body.userid);
//console.log(typeof req.body.permission);
res.json({apikey: apikey});
} catch (error) {
console.error(error);
next(error);
@ -44,26 +40,3 @@ router.post("/new", async (req, res, next) => {
module.exports = router;
/*
async function addAPIKey(userId) {
let apikey = await generateUUID()
apikey = await hashAPIKey(req.body.apikey)
let token = await apikeyModel.create({apikey, userId});
return `${token.id}-${apikey}`
}
router.post("/new", async (req, res, next) => {
try {
let apikey = await addAPIKey(req.body.userid)
res.json({apiKey: apikey})
} catch (error) {
console.error(error);
next(error);
}
});
*/