A simple and flexible JWT middleware for H3 applications. H3 is a minimal h(ttp) framework built for high performance and portability.
npm install h3-jwtA simple and flexible JWT middleware for H3 applications. H3 is a minimal h(ttp) framework built for high performance and portability.
- Features
- Installation
- Usage
- Basic usage
- Custom Token Extraction
- Configuration Options
- Testing
- Contributing
- License
- Token extraction from headers, cookies, and query parameters.
- Customizable token extraction.
- TypeScript support.
``bash`
npm install h3-jwt --save
`typescript
import {createApp, eventHandler, toNodeListener,} from "h3";
import {createServer} from "node:http";
import h3Jwt from "../src/middleware/jwtMiddleware";
const app = createApp();
const port = process.env.PORT || 3000;
const secret = process.env.JWT_SECRET || "secret"
app.use("/", eventHandler((event) => {
app.use('/hello', eventHandler((event) => {
// console.log(event._headers)
return {
message: "Hello World"
}
}))
app.use('/login', eventHandler((event) => {
// console.log(event._headers)
return {
message: "Login"
}
}))
app.use(h3Jwt({
secretKey: secret,
getToken: h3Cookie("token"),
algorithms: ["HS256"]
}))
app.use('/protected', eventHandler((event) => {
return {
message: "Protected route"
}
}))
}))
`
`typescript
import { h3Jwt, fromHeader, fromCookie } from 'h3-jwt';
app.use('/protected', h3Jwt({
secret: 'YOUR_SECRET_KEY',
getToken: (event) => {
return h3Header() || h3Cookie('cookieName') || h3Query('queryName');
}
}));
`
Detail the options users can pass to your middleware, like:
- secret (string): Secret key for JWT decoding. Required.
- getToken (Function): Custom function for token extraction. Defaults to extraction from the Authorization header.
This package is thoroughly tested. To run tests:
`bash`
npm test
Contributions are welcome! Please open an issue or submit a pull request.
This package is licensed under the MIT License. See the LICENSE` file for details.
---