delete account

This commit is contained in:
deadvey 2025-07-15 02:10:26 +01:00
parent 9a1bc97b99
commit b5ab7af515
3 changed files with 49 additions and 4 deletions

View File

@ -12,6 +12,7 @@ In action on my website: [deadvey.com](https://deadvey.com)<br/>
* hitcount
* Markdown syntax in posts
* Commenting on posts
* sign up and delete account
# Bugs
* probably scales like shit
@ -20,8 +21,8 @@ In action on my website: [deadvey.com](https://deadvey.com)<br/>
# planned features/todo list
* atom
* federation (looks tricky)
* sign up
* All strings (including in edit and post page) customisable
* formatable custom strings
* split code into files to tidy it up a bit
* inline comments and docs
* give each post a hard postID to prevent potential issues
@ -38,6 +39,7 @@ In action on my website: [deadvey.com](https://deadvey.com)<br/>
* %G - Tag name (used for the tag page only)
* %H - Frontpage hit count
* %I - User description
* %J - URL delete account
* %L - URL Permanent link to the post
* %M - comments
* %N - the username of the user (poster)

47
app.js
View File

@ -152,14 +152,17 @@ function hyperlink_tags(tags) {
// See the readme format indicators section for a full list of format indicators
// This function replaces the format indicators in a template to the content they represent
// accepts the template (string),
// the post index (int) as an optional paramter to indicate what post is to be used (for replacing things like content and titles)
// the post index (int) as an optional paramter to indicate what post is to be used (for replacing things like content and titles of posts)
// the tag (strig) as an optional parameter to indicate what tag is being used (for /tag/:tag pages)
// the user index (int) is an optional parameter to indicate what user is to be used (for replacng things like the header of the user page)
// returns the template with it's format indiactors replaced (string)
function replace_format_indicators(template, post_index=-1, tag_name="tag", user_index=-1) {
output_string = template // These should always be replaceable
.replaceAll("%%", "&#37;")
.replaceAll("%J", "/delete_account")
.replaceAll("%P", "/post")
.replaceAll("%O", `/edit/${post_index}`)
.replaceAll("%Q", "/signup")
.replaceAll("%R", "/rss")
.replaceAll("%Y", config.site_name)
.replaceAll("%W", config.site_description)
@ -357,6 +360,13 @@ app.get("/signup", (req,res) => {
console.log("Error, invalid value for allow_signup (bool)")
}
}); // /signup
app.get("/delete_account", (req,res) => {
res.send(`</html><head><meta charset="${config.charset}"><style>${config.css}</style></head><form action="/submit_delete_account" method="POST">
<input placeholder="username" required name="username"><br/>
<input placeholder="password" type="password" required id="password" name="password"><br/>
<label>${config.delete_account_confirmation}: </label><input type="checkbox" name="agreement" required><br/>
<input type="submit" value="Submit"><br/></form></html>`);
}); // /delete_account
app.get("/edit/:post_id", (req,res) => {
const post_id = req.params.post_id
const post = posts.posts[post_id]
@ -425,7 +435,7 @@ app.post("/submit_post", (req,res) => {
}
}); // /submit_post
app.post("/submit_signup", (req,res) => {
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
const username = escape_input(req.body.username)
const prettyname = escape_input(req.body.prettyname)
const description = escape_input(req.body.description)
@ -458,6 +468,39 @@ app.post("/submit_signup", (req,res) => {
console.log("Error, invalid value for allow_signup (bool)")
}
}); // /submit_signup
app.post("/submit_delete_account", (req,res) => {
// Get the form info
const password = crypto.createHash("sha512").update(req.body.password).digest("hex");
const username = escape_input(req.body.username)
// get the userID
const userID = get_userID(username)
if (userID >= 0) { // The user exists
if (password == users.users[userID]['hash']) { // password matches
console.log(username, "(userID:", userID, ") is trying deleting their account")
// Delete the user
users.users.splice(userID,1)
// Delete all their posts
for (let postid = 0; postid < posts.posts.length; postid++) { // loop over all posts
if (posts.posts[postid]['userID'] == userID) { // if userID matches
posts.posts.splice(postid,1) // delete the post
comments.comments.splice(postid,1) // the comments for this post should also be delete
}
};
// Write these changes
fs.writeFileSync(`${__dirname}/users.js`, `export const users = ${JSON.stringify(users.users)}`, 'utf-8');
fs.writeFileSync(`${__dirname}/posts.js`, `export const posts = ${JSON.stringify(posts.posts)}`, 'utf-8');
fs.writeFileSync(`${__dirname}/comments.js`, `export const comments = ${JSON.stringify(comments.comments)}\nexport const counter = ${comments.counter}`, 'utf-8');
res.redirect(301,"/")
}
else { // password does not match
res.send(`</html><head><meta charset="${config.charset}"><style>${config.css}</style></head><body>${config.incorrect_password}</body></html>`)
};
}
else {
res.send(`</html><head><meta charset="${config.charset}"><style>${config.css}</style></head><body>${config.user_doesnt_exist}</body></html>`)
}
}); // /submit_delete_account
app.post("/submit_edit", (req,res) => {
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
const postID = req.body.postID

View File

@ -1 +1 @@
464
473