

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Authentication With Passwords
incomplete
2: Password Review
incomplete
3: Types of Authentication
incomplete
4: JWTs
incomplete
5: Authentication With JWTs
incomplete
6: JWT Review
incomplete
7: Revoking JWTs
incomplete
8: Refresh Tokens
incomplete
9: Cookies
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
There are several different ways to handle authentication. We'll use JWTs in this course. They're a popular choice for APIs that are consumed by web applications and mobile apps.
A JWT is a JSON Web Token. It's a cryptographically signed JSON object that contains information about the user. You'll learn about how the cryptography of JWTs work in our Learn Cryptography course. For now, it's just important to know that once the token is created by the server, the data in the token can't be changed without the server knowing.
When your server issues a JWT to Bob, Bob can use that token to make requests as Bob to your API. Bob won't be able to change the token to make requests as Alice.
The first building blocks you'll write are the functions for creating and validating JWTs, which will be used in the next lesson to authenticate users. We'll also create some unit tests to ensure that the functions work as expected.
npm i jsonwebtoken
npm i -D @types/jsonwebtoken
import jwt from "jsonwebtoken";
function makeJWT(userID: string, expiresIn: number, secret: string): string;
Use jwt.sign(payload, secret, [options]).
A JWT payload can have any key-value pair, but I used the Pick utility function to narrow the JwtPayload type down to the keys we care about:
import type { JwtPayload } from "jsonwebtoken";
type payload = Pick<JwtPayload, "iss" | "sub" | "iat" | "exp">;
iss is the issuer of the token. Set this to chirpysub is the subject of the token, which is the user's ID.iat is the time the token was issued. Use Math.floor(Date.now() / 1000) to get the current time in seconds.exp is the time the token expires. Use iat + expiresIn to set the expirationfunction validateJWT(tokenString: string, secret: string): string;
Use the jwt.verify(token, secret) function to validate the signature of the JWT and extract the decoded token payload. It will throw an error if the token is invalid or has expired. If the token is invalid, throw a suitable error. Return the user's id from the token (which should be stored in the sub field).
npm i -D vitest@3
"scripts": {
"test": "vitest --run"
},
import { describe, it, expect, beforeAll } from "vitest";
import { makeJWT, validateJWT } from "./auth";
describe("Password Hashing", () => {
const password1 = "correctPassword123!";
const password2 = "anotherPassword456!";
let hash1: string;
let hash2: string;
beforeAll(async () => {
hash1 = await hashPassword(password1);
hash2 = await hashPassword(password2);
});
it("should return true for the correct password", async () => {
const result = await checkPasswordHash(password1, hash1);
expect(result).toBe(true);
});
});
Just add more it blocks within describe blocks to create more tests.
Run and submit the CLI tests.