JSON Web Tokens vs Sessions

Louis Trinh
2 min readJun 27, 2024
JSON Web Tokens vs Sessions

Sessions

Session-based authentication stores user information on the server in a session cookie or some other persistent storage mechanism. This information is used to verify the user’s identity on subsequent requests. Sessions have several drawbacks:

  1. Session hijacking: Session cookies can be stolen by malicious actors, allowing them to impersonate the user.
  2. Session storage overhead: Session data needs to be stored and managed on the server, which can add to the server’s load.
  3. Cross-site scripting (XSS) vulnerabilities: Sessions can be vulnerable to XSS attacks, which can allow attackers to steal session cookies and compromise user accounts.

JSON Web Tokens (JWTs)

JWTs are a JSON-based standard for securely transmitting information between parties. They consist of three segments:

  1. Header: Contains metadata about the token, such as the token type and algorithm used to sign it.
  2. Payload: Contains claims about the user, such as their identity and expiration time.
  3. Signature: Used to verify the authenticity and integrity of the token.

JWTs offer several advantages over sessions:

  1. Security: JWTs are…

--

--

Louis Trinh

JavaScript enthusiast here, sharing knowledge & building awesome things. If my articles fuel your projects, consider a coffee! buymeacoffee.com/louis.trinh