🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

h3-jwt

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

h3-jwt

A simple and flexible JWT middleware for H3 applications. H3 is a minimal h(ttp) framework built for high performance and portability.

latest
Source
npmnpm
Version
0.1.11
Version published
Maintainers
1
Created
Source

H3JWT

A simple and flexible JWT middleware for H3 applications. H3 is a minimal h(ttp) framework built for high performance and portability.

jwt compatible

Table of Contents]

Features

  • Token extraction from headers, cookies, and query parameters.
  • Customizable token extraction.
  • TypeScript support.

Installation

npm install h3-jwt --save

Usage

Basic usage

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"
            }
}))

}))

Custom Token Extraction

import { h3Jwt, fromHeader, fromCookie } from 'h3-jwt';

app.use('/protected', h3Jwt({
  secret: 'YOUR_SECRET_KEY',
  getToken: (event) => {
    return h3Header() || h3Cookie('cookieName') || h3Query('queryName');
  }
}));

Configuration Options

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.

Testing

This package is thoroughly tested. To run tests:

npm test

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This package is licensed under the MIT License. See the LICENSE file for details.

Keywords

h3

FAQs

Package last updated on 08 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts