Welcome to your mini-project challenge! 🚀
In this exercise, you’ll implement basic session-based authentication in Express.js and test it using Mocha, Chai, and Supertest.
-
POST /login- Accepts
{ username, password } - If
username = adminandpassword = secret- ✅ Save the user in the session
- ✅ Respond with 200 → { message: "Login successful" }
- Else
- ❌ Respond with 401 → { message: "Invalid credentials" }
- Accepts
-
GET /profile- If user is logged in (session exists)
- ✅ Respond with 200 → { message: "Welcome, " }
- Else
- ❌ Respond with 401 → { message: "Unauthorized" }
- If user is logged in (session exists)
-
POST /logout- Destroy the session
- ✅ Respond with 200 → { message: "Logout successful" }
1️⃣ Install Dependencies
npm install2️⃣ To start the server
npm run dev3️⃣ To test code
npm test1️⃣ Creating Session
req.session.user = { username };2️⃣ Destroying Session
req.session.destroy(err => {
if (err) {
return res.status(500).json({ message: "Logout failed" });
}
res.clearCookie("connect.sid");
})