MERN Stack Session-Based Authentication Practice

🚀 Day 34 of My MERN Stack Journey – Session-Based Authentication Practice Backend ✔ Database ✔ Sessions ✔ Authentication Logic ✔ Today I worked on a personal practice & revision project to better understand Session-based Authentication in Express.js. The goal of this project was not building a full product but strengthening my understanding of how login systems actually manage user sessions on the server. 📌 What I Practiced I created a simple authentication flow using Express Sessions and MongoDB where users can: ✔ Sign up and create an account ✔ Log in using contact number and password ✔ Generate a random session token after login ✔ Store that token in MongoDB + Express session ✔ Access a protected profile route only if authenticated This project helped me revise and reinforce session-based authentication concepts. ⚙️ Tech Stack ✔ Node.jsExpress.js ✔ MongoDB ✔ Mongoose ✔ EJS ✔ Express Session 🧠 Concepts I Focused On ✅ Session-Based Authentication Instead of JWT, this system uses server-side sessions to track logged-in users. When a user logs in: • Server generates a random token • Token stored in database • Same token stored in session req.session.token = token; 📌 Concept: Server keeps track of user identity ✅ Random Token Generation I created a custom function to generate random authentication tokens. This helped me understand how session identifiers work internally. ✅ Protected Routes Before accessing /profile, the server checks if the session token matches a user in the database. const user = await User.findOne({ token: req.session.token }); 📌 Concept: Authorization using session validation 📝 Key Learnings from This Practice 🔹 How session-based authentication works internally 🔹 How servers maintain login state using sessions 🔹 How session tokens link database users with active sessions 🔹 Clear understanding of Sessions vs JWT authentication 🔹 How protected routes verify user identity 🎯 Growth Reflection This project was mainly a revision exercise to strengthen my understanding of authentication systems. Over the last few days I practiced: ✔ Random token authentication ✔ Session-based authentication ✔ JWT authentication Now I can clearly understand how different authentication systems work under the hood. 🚀 Practice Code GitHub Repository: https://lnkd.in/dUK2Z5HZ #MERNStack #NodeJS #ExpressJS #MongoDB #Authentication #Sessions #BackendDevelopment #JavaScript #LearningInPublic

  • text

To view or add a comment, sign in

Explore content categories