Node.js
/Intermediate
Sessions & Cookies
Definition
A stateful authentication mechanism where the server stores user data in its memory (or a database/Redis) and gives the client a tiny unique ID (stored in a cookie) to reference that memory.
Explain Like I'm New
JWTs are like passports; all the information is carried by the user. Sessions are like a coat check at a club. The server takes your coat (your user data), puts it in a secure closet, and hands you a tiny numbered ticket (the Session ID Cookie). When you want your data, you hand the server the ticket, and the server looks up your data in its closet.
Real World Example
Using the `express-session` package. The user logs in, and you write `req.session.userId = user._id`. Node automatically generates a random cookie, sends it to the browser, and saves the user data in Redis.
Common Use Cases
- •Traditional monolithic web apps
- •Applications requiring strict, instant logout/revocation capabilities
Interactive Example
Loading...
Console output will appear here...
Interview Questions
basic
- What is a Cookie?
intermediate
- What is the primary difference between JWT and Session authentication?
advanced
- Why is storing Sessions in the default Node.js memory dangerous in production?