Menu

1 / 2

Week 18: JWT, Security, and Breaking My Own Auth System

DEV Community·Precious Afolabi·6 months ago
#wc36gbuw
#node#jwt#security#api#token
Reading 0:00
15s threshold

JSON Web Tokens

JWT Structure

A JWT has three parts separated by dots: header, payload, and signature. The header holds the algorithm and token type. The payload holds the user data. The signature verifies that the token has not been tampered with.

Nothing in the payload is encrypted. It is base64 encoded, which means anyone can decode it. Never put sensitive information like passwords or card numbers in a JWT.

Signing and Verifying

jwt.sign takes a payload, a secret, and options like expiry. It returns a token string. jwt.verify takes the token and the same secret. If the token is valid and unexpired, it returns the decoded payload. If not, it throws either JsonWebTokenError or TokenExpiredError, and you handle each differently.

Token Expiry

Short-lived access tokens and long-lived refresh tokens work together. The access token expires quickly, usually in 15 minutes.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.