Files
auth-server/server/auth/account-delete.js
T

60 lines
1.2 KiB
JavaScript

//libraries
const utils = require('util');
const bcrypt = require('bcryptjs');
var cron = require('node-cron');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const { accounts } = require('../database/models');
//auth/deletion
const route = async (req, res) => {
if (!req.body.password) {
return res.status(401).end('Missing password');
}
const account = await accounts.findOne({
where: {
index: req.user.index || ''
}
});
if (!account) {
return res.status(401).end('Missing account');
}
//compare the user's password
const compare = utils.promisify(bcrypt.compare);
const match = await compare(req.body.password, account.hash);
if (!match) {
return res.status(401).send('Incorrect password');
}
//set the deletion time (2 days from now)
const interval = new Date(new Date().setDate(new Date().getDate() + 2)); //wow
await accounts.update({
deletion: interval
},
{
where: {
index: req.user.index
}
});
//finally
return res.status(200).end();
};
//actually delete the accounts
cron.schedule('0 * * * *', async () => {
await accounts.destroy({
where: {
deletion: {
[Op.lt]: Sequelize.fn('NOW')
}
}
});
});
module.exports = route;