Next.js Course
Next.js
/
Intermediate

Session Authentication

Definition

A stateful authentication mechanism where the server stores active session data in a database, and the user's cookie only contains an opaque random string (Session ID).

Explain Like I'm New

Like a coat check at a club. You give the server your coat (credentials). The server hangs it on hook #42 (database row) and gives you a ticket that says '#42' (Session Cookie). When you return, the server looks up hook #42 to see who you are.

Real World Example

Highly secure applications (like banks) that require the ability to instantly 'Revoke' a session. If a user loses their laptop, the bank deletes row #42 from the database. The user's cookie immediately stops working.

Common Use Cases

  • •High-security apps
  • •Applications requiring session revocation

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • In Session Authentication, does the cookie contain the user's email address?

intermediate

  • What is the primary performance drawback of Session Authentication compared to JWT?

Flash Cards

Question

Contain email?

Click to reveal answer
Answer

No. The cookie only contains a random, meaningless Session ID string. The actual email is stored safely in the server's database.

Question

Performance drawback?

Click to reveal answer
Answer

Database hits. Every single time the user requests a page, the server must query the database to look up the Session ID to see if it is still valid. JWTs avoid this database hit.