From 2c5984d6281264984ce1f9fd56501cfa0a17b42f Mon Sep 17 00:00:00 2001 From: stringsplit <77242831+nbitzz@users.noreply.github.com> Date: Thu, 11 May 2023 15:32:00 -0700 Subject: [PATCH] hopeuflly this can fix some stuf --- src/server/lib/accounts.ts | 40 ++++++++++++++++----------------- src/server/lib/files.ts | 14 ++++++------ src/server/routes/authRoutes.ts | 20 ++++++++++------- 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/server/lib/accounts.ts b/src/server/lib/accounts.ts index 53f7395..99ec14c 100644 --- a/src/server/lib/accounts.ts +++ b/src/server/lib/accounts.ts @@ -20,23 +20,23 @@ export interface Account { defaultFileVisibility : FileVisibility } -export function create(username:string,pwd:string,admin:boolean=false) { - let accId = crypto.randomBytes(12).toString("hex") +export function create(username:string,pwd:string,admin:boolean=false):Promise { + return new Promise((resolve,reject) => { + let accId = crypto.randomBytes(12).toString("hex") - Accounts.push( - { - id: accId, - username: username, - password: password.hash(pwd), - files: [], - admin: admin, - defaultFileVisibility: "public" - } - ) + Accounts.push( + { + id: accId, + username: username, + password: password.hash(pwd), + files: [], + admin: admin, + defaultFileVisibility: "public" + } + ) - save() - - return accId + save().then(() => resolve(accId)) + }) } export function getFromUsername(username:string) { @@ -55,7 +55,7 @@ export function getFromToken(token:string) { export function deleteAccount(id:string) { Accounts.splice(Accounts.findIndex(e => e.id == id),1) - save() + return save() } export namespace password { @@ -74,7 +74,7 @@ export namespace password { if (!acc) return acc.password = hash(password) - save() + return save() } export function check(id:string,password:string) { @@ -95,7 +95,7 @@ export namespace files { if (acc.files.find(e => e == fileId)) return acc.files.push(fileId) - save() + return save() } export function deindex(accountId:string,fileId:string) { @@ -104,13 +104,13 @@ export namespace files { let fi = acc.files.findIndex(e => e == fileId) if (fi) { acc.files.splice(fi,1) - save() + return save() } } } export function save() { - writeFile(`${process.cwd()}/.data/accounts.json`,JSON.stringify(Accounts)) + return writeFile(`${process.cwd()}/.data/accounts.json`,JSON.stringify(Accounts)) .catch((err) => console.error(err)) } diff --git a/src/server/lib/files.ts b/src/server/lib/files.ts index 4847975..9992638 100644 --- a/src/server/lib/files.ts +++ b/src/server/lib/files.ts @@ -191,12 +191,6 @@ export default class Files { reject({status:500,message:"please try again"}); return } } - - // save - - if (settings.owner) { - files.index(settings.owner,uploadId) - } // this code deletes the files from discord, btw // if need be, replace with job queue system @@ -227,9 +221,15 @@ export default class Files { : undefined ), // so that json.stringify doesnt include tag:undefined - ...(ogf.tag ? {tag:ogf.tag} : {}) + ...((ogf||{}).tag ? {tag:ogf.tag} : {}) } )) + + // save + + if (settings.owner) { + await files.index(settings.owner,uploadId) + } }) } diff --git a/src/server/routes/authRoutes.ts b/src/server/routes/authRoutes.ts index 172e499..5fe84e8 100644 --- a/src/server/routes/authRoutes.ts +++ b/src/server/routes/authRoutes.ts @@ -92,15 +92,19 @@ authRoutes.post("/create", parser, (req,res) => { return } - let newAcc = Accounts.create(req.body.username,req.body.password) + Accounts.create(req.body.username,req.body.password) + .then((newAcc) => { + /* + assign token + */ - /* - assign token - */ - - res.cookie("auth",auth.create(newAcc,(3*24*60*60*1000))) - res.status(200) - res.end() + res.cookie("auth",auth.create(newAcc,(3*24*60*60*1000))) + res.status(200) + res.end() + }) + .catch(() => { + ServeError(res,500,"internal server error") + }) }) authRoutes.post("/logout", (req,res) => {