Reputation: 55729
One advantage of a JWT over a cookie seems to be that it bypasses the origin restrictions on cookies.
Can someone help me understand any other advantages and importantly any other disadvantages to JWTs?
Upvotes: 23
Views: 20104
Reputation: 1894
Advantages
JWT
is a stateless authentication mechanism as the user state is never saved in the database. As JWTs are self-contained, all the necessary information is there, reducing the need of going back and forward to the database. With JWT we don't need to query database to authenticate the user for every api call.CSRF
(Cross Site Request Forgery) attacks.myapp.com
that can make authorized client-side requests to myservice1.com
and to myservice2.com
. Cookies are bound to a single domain. A cookie created on the domain foo.com
can't be read by the domain bar.com
.Disadvantages
JWT
as it is a stateless authentication mechanism. It makes difficult to implement feature like Sign out from all devices
. This is easy to implement using session based authentication as we just need to delete the session from database.cookies
work out of the box.Upvotes: 8
Reputation: 8546
As far as I use it, a JWT is just a token used to represent data that cannot be counterfeit by the client. You can pass it to the server through a http header or through a cookie. You just need to implement on your server side both ways to access the JWT before processing it. The cookie is practical for web browser but using a header is easier for plain http request like when using curl or native apps. JWT is protocol agnostic you can also use it in a web socket, put it in a json payload or anywhere else as long as it can be accessed and decoded on the receiving side.
Upvotes: 0
Reputation: 53888
a lot of web-related info can be found in a similar post here: Token Authentication vs. Cookies; I would like to call out some "architectural" differences:
Upvotes: 12