From 60801f0dc213bb78b74226f7a1134e1597b1b5fb Mon Sep 17 00:00:00 2001 From: rainbow napkin Date: Wed, 25 Dec 2024 05:52:03 -0500 Subject: [PATCH] Alt account entries now deleted with account upon account deletion. --- .../api/account/registerController.js | 2 +- src/controllers/tooltip/altListController.js | 2 +- src/schemas/userSchema.js | 35 ++++++++++++++----- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/controllers/api/account/registerController.js b/src/controllers/api/account/registerController.js index b7d1bf7..a19db6e 100644 --- a/src/controllers/api/account/registerController.js +++ b/src/controllers/api/account/registerController.js @@ -35,7 +35,7 @@ module.exports.post = async function(req, res){ //if we found any related nuked bans if(nukedBans != null){ //Shit our pants! - return errorHandler(res, 'Cannot get alts for non-existant user!'); + return errorHandler(res, 'Cannot re-create banned account!', 'unauthorized'); } await userModel.register(user, req.ip); diff --git a/src/controllers/tooltip/altListController.js b/src/controllers/tooltip/altListController.js index d192d48..b40896c 100644 --- a/src/controllers/tooltip/altListController.js +++ b/src/controllers/tooltip/altListController.js @@ -31,7 +31,7 @@ module.exports.get = async function(req, res){ const userDB = await userModel.findOne({user: data.user}); if(userDB == null){ - return errorHandler(res, 'Cannot re-create banned account!', 'unauthorized'); + return errorHandler(res, 'Cannot get alts for non-existant user!'); } return res.render('partial/tooltip/altList', {alts: await userDB.getAltProfiles()}); diff --git a/src/schemas/userSchema.js b/src/schemas/userSchema.js index 3d2b8d3..730706c 100644 --- a/src/schemas/userSchema.js +++ b/src/schemas/userSchema.js @@ -187,6 +187,27 @@ userSchema.pre('save', async function (next){ next(); }); +//post-delete function (document not query) +userSchema.post('deleteOne', {document: true}, async function (){ + //Kill any active sessions + await this.killAllSessions("If you're seeing this, your account has been deleted. So long, and thanks for all the fish! <3"); + + //Populate alts + await this.populate('alts'); + + //iterate through alts + for(alt in this.alts){ + //Find the index of the alt entry for this user inside of the alt users array of alt users + const altIndex = this.alts[alt].alts.indexOf(this._id); + + //splice the entry for this user out of the alt users array of alt users + this.alts[alt].alts.splice(altIndex,1); + + //Save the alt user + await this.alts[alt].save(); + } +}); + //statics userSchema.statics.register = async function(userObj, ip){ //Pull values from user object @@ -370,7 +391,7 @@ userSchema.statics.processAgedIPRecords = async function(){ //methods userSchema.methods.checkPass = function(pass){ - return hashUtil.comparePassword(pass, this.pass); + return hashUtil.comparePassword(pass, this.pass) } userSchema.methods.getAuthenticatedSessions = async function(){ @@ -606,20 +627,18 @@ userSchema.methods.passwordReset = async function(passChange){ } userSchema.methods.nuke = async function(pass){ + //Check we have a confirmation password if(pass == "" || pass == null){ + //scream and shout throw new Error("No confirmation password!"); } + //Check that the password is correct if(this.checkPass(pass)){ - //Annoyingly there isnt a good way to do this from 'this' + //delete the user var oldUser = await this.deleteOne(); - - if(oldUser){ - await this.killAllSessions("If you're seeing this, your account has been deleted. So long, and thanks for all the fish! <3"); - }else{ - throw new Error("Server Error: Unable to delete account! Please report this error to your server administrator, and with timestamp."); - } }else{ + //complain about a bad pass throw new Error("Bad pass."); } }