Skip to content

Commit b7ddd16

Browse files
committed
update README -- add quick start
- fix bold issue at 'supported algorithms' secion
1 parent 2151601 commit b7ddd16

File tree

1 file changed

+110
-14
lines changed

1 file changed

+110
-14
lines changed

README.md

Lines changed: 110 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ A secure and tested json-web-token class-based utility library for generating ke
2020

2121
- [✨ Features](#-features)
2222
- [📥 Installation](#-installation)
23+
- [🚀 Quick Start](#-quick-start)
2324
- [✅ Usage](#-usage)
2425
- [🔑 Key Creation](#-key-creation)
2526
- [🏗️ JWT Instance Creation](#️-jwt-instance-creation)
@@ -39,8 +40,8 @@ A secure and tested json-web-token class-based utility library for generating ke
3940
- ⌚ Time helper functions from [@bepalo/time](#bepalotime).
4041
- 📄 Written in modern TypeScript.
4142
- 📢 Available for both ESM and CommonJS.
42-
- 📢 Works with node, bun, and deno.
43-
- 📢 Built on crypto API.
43+
- 📢 Works with Node, Bun, and Deno.
44+
- 📢 Built on the crypto API.
4445

4546
## 📥 Installation
4647

@@ -57,6 +58,8 @@ Pnpm:
5758
pnpm add @bepalo/jwt
5859
```
5960

61+
>Requires Node.js v18.0.0 or newer.
62+
6063
Npm
6164

6265
```sh
@@ -69,17 +72,111 @@ Deno:
6972
import { JWT } from "jsr:@bepalo/jwt"
7073
```
7174
75+
## 🚀 Quick Start
76+
77+
**Simple Symmetric HMAC:**
78+
79+
```ts
80+
import { JWT } from "@bepalo/jwt";
81+
82+
// 🏁 1. Generate a key
83+
const secret = JWT.genHmac("HS256");
84+
85+
// 🏁 2. Store the generated key somewhere safe like in a .env file
86+
console.log(secret);
87+
88+
// 🏁 3. Load that key from where it was stored
89+
const signKey = process.env.SECRET;
90+
const verifyKey = process.env.SECRET;
91+
92+
// 🏁 4. Create a JWT instance for signing
93+
const jwtSign = JWT.createSymmetric(signKey, "HS256");
94+
95+
// 🏁 5. Sign a payload
96+
const token = jwtSign.signSync({
97+
userId: 123,
98+
role: "admin",
99+
jti: "tid-1234",
100+
iat: JWT.now(),
101+
// exp: JWT.on("2026"),
102+
// nbf: JWT.after(5).Minutes,
103+
// ...
104+
});
105+
106+
// 🏁 6. Create another JWT instance for verifying. *optional*
107+
const jwtVerify = JWT.createSymmetric(verifyKey, "HS256");
108+
109+
// 🏁 7. Verify and decode the token
110+
const { valid, payload, error } = jwtVerify.verifySync(token, {
111+
jti: "tid-1234",
112+
nbfLeeway: JWT.for(5).Seconds
113+
});
114+
115+
// 🏁 8. Deal with errors or use the payload
116+
console.log(valid); // true
117+
console.log(payload); // { userId: 123, role: "admin", ... }
118+
console.log(error); // undefined
119+
```
120+
121+
**Generic:**
122+
123+
```ts
124+
import { JWT } from "@bepalo/jwt";
125+
126+
// 🏁 1. Generate a key
127+
const key = JWT.genKey("ES256");
128+
129+
// 🏁 2. Store the generated key somewhere safe like in a .env file
130+
const { alg, publicKey, privateKey } = key;
131+
console.log(JSON.stringify({ alg, publicKey }));
132+
console.log(JSON.stringify({ alg, privateKey }));
133+
134+
// 🏁 3. Load that key from where it was stored
135+
const signKey = JSON.parse(process.env.PRIVATE_KEY ?? "null");
136+
const verifyKey = JSON.parse(process.env.PUBLIC_KEY ?? "null");
137+
138+
// 🏁 4. Create a JWT instance for signing
139+
const jwtSign = JWT.create(signKey);
140+
141+
// 🏁 5. Sign a payload
142+
const token = jwtSign.signSync({
143+
userId: 123,
144+
role: "admin",
145+
jti: "tid-1234",
146+
iat: JWT.now(),
147+
// exp: JWT.on("2026"),
148+
// nbf: JWT.after(5).Minutes,
149+
// ...
150+
});
151+
152+
// 🏁 6. Create a JWT instance for verifying
153+
const jwtVerify = JWT.create(verifyKey);
154+
155+
// 🏁 7. Verify and decode the token
156+
const { valid, payload, error } = jwtVerify.verifySync(token, {
157+
jti: "tid-1234",
158+
nbfLeeway: JWT.for(5).Seconds
159+
});
160+
161+
// 🏁 8. Deal with errors or use the payload
162+
console.log(valid); // true
163+
console.log(payload); // { userId: 123, role: "admin", ... }
164+
console.log(error); // undefined
165+
166+
```
167+
168+
72169
## ✅ Usage
73170
74171
### 🔑 Key Creation
75172
76173
```ts
77174
import { JWT } from "@bepalo/jwt";
78175

79-
// Symmetric HMAC key generation. returns string
176+
// 📢 Symmetric HMAC key generation. returns string
80177
const secret = JWT.genHmac("HS256");
81178

82-
// Generic way of generating any key. returns JwtKey
179+
// 📢 Generic way of generating any key. returns JwtKey
83180
const key = JWT.genKey("none");
84181
const key = JWT.genKey("HS512");
85182
const key = JWT.genKey("ES384");
@@ -95,12 +192,12 @@ import { JWT } from "@bepalo/jwt";
95192

96193
type Payload = { userId: number, role: "admin" | "user" };
97194

98-
// Symmetric only way of creating a JWT instance.
99195
const secret = JWT.genHmac("HS256");
196+
// 📢 Symmetric only way of creating a JWT instance.
100197
const jwt = JWT.createSymmetric<Payload>(secret, "HS256");
101198

102-
// Generic way of creating a JWT instance
103199
const key = JWT.genKey("ES256");
200+
// 📢 Generic way of creating a JWT instance
104201
const jwt = JWT.create<Payload>(key);
105202
```
106203
@@ -116,7 +213,7 @@ type Payload = { userId: number, role: "admin" | "user" };
116213
const key = JWT.genKey("HS256");
117214
const jwt = JWT.create<Payload>(key);
118215

119-
// Sign synchronously
216+
// 📢 Sign synchronously
120217
const token = jwt.signSync({ userId: 123, role: "admin", iat: JWT.now() });
121218
```
122219
@@ -130,7 +227,7 @@ type Payload = { userId: number, role: "admin" | "user" };
130227
const key = JWT.genKey("HS256");
131228
const jwt = JWT.create<Payload>(key);
132229

133-
// Sign asynchronously
230+
// 📢 Sign asynchronously
134231
const token = await jwt.sign({ userId: 123, role: "admin", iat: JWT.now() });
135232
```
136233
@@ -148,10 +245,10 @@ const jwt = JWT.create<Payload>(key);
148245

149246
const token = jwt.signSync({ userId: 123, role: "admin", iat: JWT.now() });
150247

151-
// Verify synchronously
248+
// 📢 Verify synchronously
152249
const { valid, payload, error } = jwt.verifySync(token);
153250

154-
// Verify signature synchronously
251+
// 📢 Verify signature synchronously
155252
const { valid, error } = jwt.verifySignatureSync(token);
156253
```
157254
@@ -167,10 +264,10 @@ const jwt = JWT.create<Payload>(key);
167264

168265
const token = await jwt.sign({ userId: 123, role: "admin", iat: JWT.now() });
169266

170-
// Verify asynchronously
267+
// 📢 Verify asynchronously
171268
const payload = await jwt.verify(token);
172269

173-
// Verify signature asynchronously
270+
// 📢 Verify signature asynchronously
174271
const valid = await jwt.verifySignature(token);
175272
```
176273
@@ -498,7 +595,7 @@ But you can use `ES256` or `ES384`, or switch to a runtime that supports it.
498595
499596
<details>
500597
501-
<summary><b>RSA-PSS (Asymmetric): RSA-PSS variants.<b></summary>
598+
<summary><b>RSA-PSS (Asymmetric): RSA-PSS variants.</b></summary>
502599
503600
- PS256: RSA-PSS variant with SHA-256.
504601
- PS384: RSA-PSS variant with SHA-384.
@@ -519,7 +616,6 @@ For more details, see [RFC 7519](https://datatracker.ietf.org/doc/html/rfc7519)
519616
## 🕊️ Thanks, Stay Safe and Enjoy
520617
521618
If you like this library and want to support then please give a star on [GitHub ![GitHub Repo stars](https://img.shields.io/github/stars/bepalo/jwt?style=social)](https://github.com/bepalo/jwt)
522-
.
523619
524620
<details>
525621
<summary> Easter egg</summary>

0 commit comments

Comments
 (0)