DEV Community

Discussion on: Node.js + Angular 11: JWT Authentication & Authorization example

Collapse
jdforsythe profile image
Jeremy Forsythe

This is dangerous. The JWT should not be available to the client side code at all, let alone stored in SessionStorage.

The proper way is to set the JWT as a secure, http-only cookie that isn't accessible to the client code. It removes the need for the token storage service and interceptor, because the cookie is sent automatically, and it removes the chance of the token being stolen and someone impersonating your users.

Collapse
anduser96 profile image
Andrei Gatej

Apart from XSS attacks, which are less likely to happen IMO if you're careful with the libraries you're using, what would be other downsides to storing the token in LocalStorage?

Also, if you go with the cookie approach, you'll have to make sure that you prevent CSRF attacks from happening.

Collapse
tis profile image
Tomasz Struczyński

See my comment, for example session impersonation etc.

Collapse
justintime4tea profile image
Justin Gross • Edited on

This is another example of a tutorial where it is fine for learning though not for learning how to do auth in production.

Please do not roll you're own auth in production. If you want to prototype something quickly then sure, roll your own auth, otherwise you should be using the defacto auth standard OpenID. Do not implement OpenID yourself there are a ton of providers and several good open source servers.

Do auth right, for your users sake.