DEV Community

What really is the difference between session and token based authentication

Brian Iyoha on January 13, 2019

A friend who is just getting into using Nodejs for backend development asked me to explain the difference between using session and jwt. So I tho...
Collapse
 
anamikaahmed profile image
AnamikaAhmed

Best Explanation On Internet

Collapse
 
thecodearcher profile image
Brian Iyoha

Thanks. I'm happy you liked it.

Collapse
 
user_27757d4934 profile image
user_27757d4934

I read ur article on medium. Good job.

Collapse
 
saidbakr profile image
Said Bakr

How to secure the JWT data? for example, in the session, user_id, shopping cart items, etc are stored on the server, while in JWT they are stored on the client, so the client may be able to change his user_id to 1 for example to gain super admin permissions later. This is a messy point to me!

Collapse
 
goose97 profile image
Nguyễn Văn Đức

JWT implementation already deals with that. Simply put, anyone can read (decode) the token. (the encoding scheme is Base64). However, it's impossible to forge a new valid token like your situation without the authenticating server knowing about it. The fake token on subsequent requests will be rejected immediately.

Collapse
 
webdevopsfresher profile image
webdevops-fresher

Even if a user tampers a token stored on client side,the server will compare the token sent with each subsequent request with it's secret key.

Collapse
 
saidbakr profile image
Said Bakr

@goose97 @webdevopsfresher
_It is too late, but thank you for your reply. _
This may explain why such kinds of authentication need the HTTPS? I think, to add encryption as an additional security layer between the client and the server.

Collapse
 
risafj profile image
Risa Fujii • Edited

Thanks for a great intro to this topic!

Collapse
 
sentisso profile image
SenTisso • Edited

Please... for the love of god, never store the JWT in localStorage nor sessionStorage. It is vulnerable to XSS and a ton of other stuff. Store it in a secure cookie and let the server handle it without any client manipulation.
Anyway this is a great explanation!

Collapse
 
doncitytech profile image
Princewill Opara

I love the simplicity, thanks

Collapse
 
saucekode profile image
Chiamaka Mbah

Hey! This is the best explanation. Thank you!

Collapse
 
jsonlisky profile image
jsonlisky

What is the validity period of the token and how to ensure it is active

Collapse
 
dominuskelvin profile image
Kelvin Omereshone

Hey, you get to set the validity when implementing the token on your server.

Collapse
 
iamadou profile image
Ibrahim AMADOU

Thanks

Collapse
 
sujithvsuresh profile image
Sujith V S

great

Collapse
 
aderchox profile image
aderchox

how do we know the jwt received is right if we don't store it on the server side ? Do we encrypt it with our own private keys and decrypt them back afterwards?

Collapse
 
chandelieraxel profile image
Chandelier Axel

Hey ! Pretty much, yes. Basically, once the server create the JWT, it'll "sign" it with a secured secret (an overcomplicated string, most likely). When your client send the JWT with the request, the server will "verify" the token, using the secret key you used to sign it.

Collapse
 
turdialiyev profile image
G'olibjon

prefect bro