Skip to content

Commit

Permalink
Merge pull request agiledev-students-fall2023#168 from agiledev-stude…
Browse files Browse the repository at this point in the history
…nts-fall2023/user_story_#80

Finish logout & del account
  • Loading branch information
Ceiceiceii authored Nov 28, 2023
2 parents 06871da + c2d3e8b commit 2dfaa0e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
10 changes: 5 additions & 5 deletions back-end/src/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
30 changes: 26 additions & 4 deletions back-end/src/routes/delaccountRouter.mjs
Original file line number Diff line number Diff line change
@@ -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 });
}
}
}

Expand Down
25 changes: 0 additions & 25 deletions back-end/src/routes/hashedPassword.mjs

This file was deleted.

15 changes: 9 additions & 6 deletions front-end/src/pages/Account/Account.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '[email protected]');
// console.log(localStorage.getItem('user'))
console.log(storedUserData.email)

// Set username and email on the screen
Expand Down Expand Up @@ -135,7 +134,7 @@ const AccountEdit = (props) => {
}
}

// route /resetpassword
// Finished: route /resetpassword
const confirmResetPassword = async (evt) => {
try {
evt.preventDefault()
Expand Down Expand Up @@ -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": {
Expand Down

0 comments on commit 2dfaa0e

Please sign in to comment.