Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ Base URL: `http://localhost:5000`
| `PUT` | `/api/profile` | Update the current user's profile | ✅ |
| `GET` | `/api/search/teammates` | Search teammates by skill & availability | ✅ |
| `GET` | `/api/teammates` | Paginated teammate list | ✅ |
| `DELETE` | `/api/profile` | Permanently delete the current user's account | ✅ |
| `DELETE` | `/api/teammates/:id` | Delete a teammate entry | ✅ |

**Example — search for ML developers:**

Expand Down
36 changes: 35 additions & 1 deletion public/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,22 @@ document.addEventListener("DOMContentLoaded", () => {
<p style="color:#eee; font-size:0.95rem; margin-bottom:10px;">${escapeHtml(u.bio || "No bio provided yet.")}</p>
<div><strong>Skills:</strong> ${skillsBadgeHtml || "<em>None added</em>"}</div>
</div>
<button id="profileLogoutBtn" class="logout-btn" style="cursor:pointer;">Logout</button>
<div style="display:flex; gap:10px;">
<button id="profileLogoutBtn" class="logout-btn" style="cursor:pointer;">Logout</button>
<button id="deleteAccountBtn" class="logout-btn" style="cursor:pointer; background:rgba(220,53,69,0.25); border:1px solid rgba(220,53,69,0.6); color:#ff8b8b;">Delete Account</button>
</div>
</div>
`;

const profileLogoutBtn = document.getElementById("profileLogoutBtn");
if (profileLogoutBtn) {
profileLogoutBtn.addEventListener("click", handleLogout);
}

const deleteAccountBtn = document.getElementById("deleteAccountBtn");
if (deleteAccountBtn) {
deleteAccountBtn.addEventListener("click", handleDeleteAccount);
}
} catch (err) {
console.error("Profile load error:", err);
profileDiv.innerHTML = `<p style="color:#ff8b8b;">Error loading profile: ${escapeHtml(err.message)}</p>`;
Expand All @@ -114,6 +122,32 @@ document.addEventListener("DOMContentLoaded", () => {
}
}

// -----------------------------------------------------------
// Delete Account Handler
// -----------------------------------------------------------
async function handleDeleteAccount() {
const confirmed = window.confirm(
"Are you sure you want to permanently delete your account? This cannot be undone."
);
if (!confirmed) return;

try {
const res = await fetch("/api/profile", {
method: "DELETE",
headers: { Authorization: `Bearer ${token}` }
});
const data = await res.json();
if (!data.success) {
throw new Error(data.message || "Failed to delete account");
}
localStorage.removeItem("authToken");
window.location.href = "signup.html";
} catch (err) {
console.error("Delete account error:", err);
window.alert("Error deleting account: " + err.message);
}
}

// -----------------------------------------------------------
// Teammates Fetch & Render
// -----------------------------------------------------------
Expand Down
62 changes: 62 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,36 @@ app.put("/api/profile", authenticateUser, async (req, res) => {
}
});

// Delete Account (permanent — removes the user and all their sessions)
app.delete("/api/profile", authenticateUser, async (req, res) => {
try {
const userId = req.user._id || req.user.id;

// 1. MongoDB Mode
if (mongoose.connection.readyState === 1) {
await Session.deleteMany({ userId });
await User.findByIdAndDelete(userId);
return res.json({ success: true, message: "Account deleted successfully" });
}

// 2. In-Memory Mode
const idx = inMemoryUsers.findIndex(u => u.id === userId || u._id === userId);
if (idx !== -1) {
inMemoryUsers.splice(idx, 1);
}
Object.keys(inMemorySessions).forEach((t) => {
if (inMemorySessions[t].userId === userId) {
delete inMemorySessions[t];
}
});

res.json({ success: true, message: "Account deleted successfully" });
} catch (error) {
console.error("Delete account error:", error);
res.status(500).json({ success: false, message: "Error deleting account" });
}
});

// Logout
app.post("/api/logout", authenticateUser, async (req, res) => {
try {
Expand Down Expand Up @@ -610,6 +640,38 @@ app.get("/api/teammates", async (req, res) => {
}
});

// Delete a Teammate entry (moderation / cleanup)
app.delete("/api/teammates/:id", authenticateUser, async (req, res) => {
try {
const { id } = req.params;

// 1. MongoDB Mode
if (mongoose.connection.readyState === 1) {
try {
const deleted = await Teammate.findByIdAndDelete(id);
if (!deleted) {
return res.status(404).json({ success: false, message: "Teammate not found" });
}
return res.json({ success: true, message: "Teammate deleted successfully" });
} catch (mongoErr) {
console.warn("MongoDB teammate delete error, falling back to memory:", mongoErr.message);
}
}

// 2. In-Memory Mode
const idx = inMemoryTeammates.findIndex(t => t.id === id || t._id === id);
if (idx === -1) {
return res.status(404).json({ success: false, message: "Teammate not found" });
}
inMemoryTeammates.splice(idx, 1);

res.json({ success: true, message: "Teammate deleted successfully" });
} catch (error) {
console.error("Delete teammate error:", error);
res.status(500).json({ success: false, message: "Error deleting teammate" });
}
});

// Database status (diagnostic endpoint)
app.get("/api/db-status", authenticateUser, async (req, res) => {
try {
Expand Down
Loading