Hello guys I'm working on a "social Media" application, and today I made change username route. Everything works fine but I need to make username "unchangable" for * days after it is changed. Can someone tell me how to do it?
Code:
AuthRouter.post("/change-username", Authorize, async (req, res) => {
const { username } = req.body
try {
if(!username) {
return res.status(400).json("Please fill in all fields")
}
const previousUsername = await pool.query(
"SELECT user_name FROM users WHERE user_id = $1",
[req.user.id]
)
const checkNewUsername = await pool.query("SELECT * FROM users WHERE user_name = $1", [
previousUsername
])
if (checkNewUsername.rows.length > 0) {
return res.status(401).json("Email or Username is already in use")
}
await pool.query("UPDATE users SET user_name = $1 WHERE user_id = $2 RETURNING *", [
username, req.user.id
])
return res.status(200).json("Username changed")
} catch (error) {
console.error(error)
}
})
Note: I'm not telling you to write my code but to tell me some ways how should I do it