Open-source project
dwyl/learn-json-web-tokens avatar
dwyl/learn-json-web-tokens

dwyl/learn-json-web-tokens: A Node.js JWT Tutorial With Four Endpoints, a Shared Secret and 100% Coverage Gates

:closed_lock_with_key: Learn how to use JSON Web Token (JWT) to secure your next Web App! (Tutorial/Example with Tests!!)

4,171 stars242 forksJavaScriptMIT

At a glance

What is it?
A teaching repository that explains JSON Web Tokens and backs the explanation with a small Node.js server and tests. It is built for learning the shape of a JWT, not for copying into a production login system.
Who is it for?
This repository suits developers meeting JWTs for the first time who want a short explanation and a server small enough to read in one sitting. It is a teaching example rather than a production identity system: the example stores token validity in LevelDB and checks it asynchronously, but uses hard-coded credentials.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 3 days ago.
What is it written in?
Mainly JavaScript, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 23, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • dwyl/learn-json-web-tokens4,171 ★ · -1
Star history
Repository2026-09-232026-11-23
dwyl/learn-json-web-tokens4.2k4.2k

-1 Stars in the last 61 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

The pitch is read-only signed claims, which means the payload is never a secret

The tutorial's core claim is that a JWT lets you send read-only signed claims between services. Claims are any bits of data you want someone else to be able to read or verify but not alter. The wording matters because it sets the limit: a signed token proves the content was not changed, it does not hide the content.

The consequence for a reader is blunt. Anything you place in the payload can be read by whoever holds the token, so the advice in the repository to store the bare minimum in the JWT is also a privacy rule, not just a size rule. The payload length grows in proportion to the data you store, and the token travels in a header or even in a URL, as the sample link with a token query parameter shows. Do not put passwords or private personal data in it.

Three parts: the signature protects header and payload

A token is three url-safe strings separated by periods, shown in the tutorial across several lines for readability but used as a single string:

code
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9           // header
.eyJrZXkiOiJ2YWwiLCJpYXQiOjE0MjI2MDU0NDV9      // payload
.eUiabuiKv-8PYk2AkGY4Fb5KMZeorYBLw261JPQD5lM   // signature

The header is an encoded representation of a simple JavaScript object that names the hashing algorithm used. The payload is the core of the token. The signature is generated from the header and the payload and is used to verify that the JWT is valid. Encoding is not encryption, so the first two parts are readable by anyone. What the signature buys you is detection of changes, which only works while the secret stays on the server.

Claims are a short fixed vocabulary, and the tutorial itself calls two of them rarely used

The tutorial lists the predefined keys. iss is the issuer. exp is the expiration timestamp, used to reject expired tokens, and the repository notes that the spec requires it in seconds. iat is the time the token was issued and can be used to work out its age. nbf, short for not before, is a future time at which the token becomes active. jti is a unique token identifier that can support replay prevention when a verifier tracks and checks it; the claim alone does not stop reuse. sub is the subject and aud the audience, both labeled rarely used.

The seconds rule is the one that bites. JavaScript's Date works in milliseconds, which is why the example code divides by 1000 with Math.floor before adding a week. A value computed in milliseconds is read as a timestamp far in the future, so a token you meant to expire in a week would effectively never expire. The reference given is section 4.1 of RFC 7519.

The example server has four endpoints and one hard-coded notion of who you are

The example under the example directory uses the core node.js http server and defines four endpoints in server.js. /home serves the page holding the login form. /auth authenticates the visitor and returns an error plus the login form on failure. /private is the restricted content and requires a valid session token. /logout invalidates the token and logs the user out so an old token cannot be re-used.

The repository says it kept server.js deliberately simple for readability, maintainability and testability, with every helper and handler tested separately. The example authenticates against hard-coded credentials, creates a GUID for each token, and stores its validity in LevelDB. Validation checks the signed auth claim against that store, and logout marks the matching record invalid. This is a teaching example rather than a per-user identity system.

generateToken signs with a secret from JWT_SECRET and a seven-day expiry

The actual example signs a token with an `auth` identifier and the request user-agent using the secret from `JWT_SECRET`. It uses the jsonwebtoken `expiresIn` option, defaulting to seven days. A wrapper creates the identifier and stores a validity record in LevelDB; validation consults that record after verifying the signature.

The identifier is generated from the current time, and the source itself notes that a stronger generator is needed in practice. The example also uses hard-coded login credentials, so use it to study the flow rather than as a production authentication template.

Signature verification and the asynchronous validity check

The implementation verifies the JWT signature synchronously with a local secret, then looks up the decoded auth identifier in LevelDB asynchronously. A missing or invalid record causes authentication to fail; logout updates the stored validity record. This stateful check is how the example supports revocation, but its hard-coded credentials and simplified error handling make it a tutorial rather than a production authentication design.

The tests enforce 100% coverage, but the dependency list is from an older Node era

Tests live in example/test. functional.js exercises the helper methods and integration.js simulates the requests a user would send and checks the responses, with a hand-written mock of the http request and response objects in mock.js. The package.json coverage script runs istanbul check-coverage with statements, functions, lines and branches each set to 100, and a pre-commit hook runs jshint and coverage.

The runtime dependencies are jsonwebtoken ^8.5.1 and level ^5.0.1, with tape, istanbul, tap-spec, request and jshint for development, and the engines field asks for Node 6 or newer. The package version is 1.0.6, the repository has no GitHub releases, and the last push was on 2026-08-15. The license is MIT on the repository but ISC in package.json. The badges point at Travis CI, so do not assume the continuous integration they show is still running.

Editorial conclusion

This repository suits developers meeting JWTs for the first time who want a short explanation and a server small enough to read in one sitting. It is a teaching example rather than a production identity system: the example stores token validity in LevelDB and checks it asynchronously, but uses hard-coded credentials. Check the age of its dependencies before running it, and treat the example as a diagram, not a template.

Frequently asked questions

How do you get a JSON Web Token in the learn-json-web-tokens example?

The generateToken helper calls jwt.sign when a visitor authenticates at /auth. The token is sent back to the client and used in the Authorization header on later requests.

What are JSON Web Tokens used for, according to dwyl/learn-json-web-tokens?

They carry read-only signed claims between services, and they identify or authenticate users in a web or mobile app by proving the user has logged in and may access the content.

How does the learn-json-web-tokens example decode a JSON Web Token?

The example verifies the Authorization header token with jwt.verify and a shared secret, then checks the decoded auth identifier against an asynchronously retrieved LevelDB validity record. Invalid signatures, expired tokens and missing or invalid records are rejected.

What are the 3 components of a JWT token?

A header describing the token and its hashing algorithm, a payload holding the claims, and a signature generated from the header and payload that verifies the token is valid.

Official sources

  1. dwyl/learn-json-web-tokens on GitHub
  2. Issues
  3. License: MIT
  4. README
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/dwyl-learn-json-web-tokens.svg)](https://hysenlabs.com/projects/dwyl-learn-json-web-tokens)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/dwyl-learn-json-web-tokens.svg)](https://hysenlabs.com/projects/dwyl-learn-json-web-tokens)

Was this useful?