diff --git a/back-end/src/app.mjs b/back-end/src/app.mjs index b7087e0..05cb7ad 100644 --- a/back-end/src/app.mjs +++ b/back-end/src/app.mjs @@ -77,7 +77,7 @@ console.log('Session secret:', process.env.SESSION_SECRET); app.post("/register", registerRouter) app.post("/login", loginRouter); -// Validation rules for changeusernameRouter, resetemailRouter +// Validation rules for routers const usernameValidationRules = [ body('newUsername') .trim() // Removes leading and trailing spaces @@ -109,11 +109,11 @@ const passwordValidationRules = [ // routes that needs authentication // Account routes -app.patch("/changeusername", usernameValidationRules, changeusernameRouter); -app.patch("/resetemail", emailValidationRules, resetemailRouter); +app.patch("/changeusername", usernameValidationRules, changeusernameRouter); //Finished +app.patch("/resetemail", emailValidationRules, resetemailRouter); //Finished app.post("/forgetpassword", forgetpasswordRouter); -app.patch("/resetpassword", passwordValidationRules, resetpasswordRouter); -app.delete("/delaccount", delaccountRouter); +app.patch("/resetpassword", passwordValidationRules, resetpasswordRouter); //Finished +app.delete("/delaccount", delaccountRouter); //Finished // Favorites list routes app.get('/getfavlist', favListRouter); diff --git a/back-end/src/routes/delaccountRouter.mjs b/back-end/src/routes/delaccountRouter.mjs index 370bade..4fc44dd 100644 --- a/back-end/src/routes/delaccountRouter.mjs +++ b/back-end/src/routes/delaccountRouter.mjs @@ -1,9 +1,31 @@ -const delaccountRouter = (req, res) => { +import User from "../models/User.mjs"; +import jwt from 'jsonwebtoken'; + +const delaccountRouter = async(req, res) => { try { - // TODO: delete account and favlist in database - return res.sendStatus(200) + if (!req.headers.authorization) { + return res.status(401).json({ message: "Authorization token is missing" }); + } + + const token = req.headers.authorization.split(' ')[1]; + const decoded = jwt.verify(token, process.env.JWT_SECRET); + const userID = decoded.id; + console.log(userID) + + // Find the user + const user = await User.findOne({ uuid: userID }); + if (!user) { + return res.status(404).json({ message: "User not found" }); + } + + await User.deleteOne({ uuid: userID }); + res.status(200).json({ message: "Account successfully deleted" }); } catch (error) { - + if (error.name === "JsonWebTokenError") { + res.status(401).json({ message: "Invalid Token" }); + } else { + res.status(500).json({ message: "Server error occurred", error }); + } } } diff --git a/back-end/src/routes/hashedPassword.mjs b/back-end/src/routes/hashedPassword.mjs deleted file mode 100644 index 27e06d5..0000000 --- a/back-end/src/routes/hashedPassword.mjs +++ /dev/null @@ -1,25 +0,0 @@ -import bcrypt from 'bcryptjs'; -import { v4 as uuidv4 } from 'uuid'; - -mongoose.connect('mongodb://localhost:27017/bakerdb', { useNewUrlParser: true, useUnifiedTopology: true }) - .then(() => console.log('Connected to MongoDB...')) - .catch(err => console.error('Could not connect to MongoDB...', err)); - -//Test hashed password to save hased password into db -const password = "password123"; -const saltRounds = 10; -bcrypt.hash(password, saltRounds, function(err, hash) { - if (err) { - console.error(err); - return; - } - console.log("Hashed password:", hash); -}); - -//Create 1st user in db -db.users.insertOne({ - uuid: uuidv4(), // This generates a unique UUID - username: "John Doe", - email: "email@nyu.edu", - password: "$2a$10$9STrp3fHvjPnJ3r8PH1WwejDAdf.uegxqYnefmu4QtaAC/bIyqPrS" -}); \ No newline at end of file diff --git a/front-end/src/pages/Account/Account.jsx b/front-end/src/pages/Account/Account.jsx index ec063c8..7a1ab57 100644 --- a/front-end/src/pages/Account/Account.jsx +++ b/front-end/src/pages/Account/Account.jsx @@ -22,7 +22,6 @@ const AccountEdit = (props) => { const storedUserData = JSON.parse(localStorage.getItem('user') || '{}'); const [username, setUsername] = useState(storedUserData.name || 'John Doe'); const [email, setEmail] = useState(storedUserData.email || 'Asdfasdfasdf@nyu.edu'); - // console.log(localStorage.getItem('user')) console.log(storedUserData.email) // Set username and email on the screen @@ -135,7 +134,7 @@ const AccountEdit = (props) => { } } - // route /resetpassword + // Finished: route /resetpassword const confirmResetPassword = async (evt) => { try { evt.preventDefault() @@ -163,30 +162,34 @@ const AccountEdit = (props) => { } } - // TODO: clear user data in local storage + // Finished const confirmLogOutAccount = async (evt) => { + // Clear all local storage data + localStorage.clear(); navigate("/", { state: { from: location.pathname } }); } + // Finished const deleteAccount = (evt) => { - setCurrentActionData(confirmDelAccount) + setCurrentActionData(confirmDeleteAccount) } - // TODO: delete user account + // Finished const confirmDeleteAccount = async (evt) => { const requestData = {}; - requestData["userID"] = "1234" try { await axiosProvider.delete( "/delaccount", requestData ) + localStorage.clear(); navigate("/", { state: { from: location.pathname } }); } catch (error) { const errorMessage = error?.requestMessage || error.response?.data?.message || 'Change failed, please try again.'; setMessage(errorMessage); } } + //All PopupContent data const formData = { "changeUsername": {