# jwt-validator-js

> A library that validates JWT token as middleware

Latest version **0.0.3** (published 2023-11-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install jwt-validator-js
pnpm add jwt-validator-js
yarn add jwt-validator-js
bun add jwt-validator-js
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2023-11-11 |
| First published | 2023-11-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 13.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Moses Wangira |
| Maintainers | moses-wangira |

## Links

- npm: https://www.npmjs.com/package/jwt-validator-js
- npm.io page: https://npm.io/package/jwt-validator-js

## Dependencies (1)

- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^9.0.1

## Recent versions

- 0.0.3 (latest) — 2023-11-11
- 0.0.2 — 2023-11-11
- 0.0.1 — 2023-11-11

## README

## JWT Validator Middleware

This middleware provides a simple and secure way to authenticate requests using JSON Web Tokens (JWT). It extracts the token from the Authorization header, verifies it using the provided secret, and attaches the decoded user information to the request object. If the token is invalid or has expired, an appropriate error response is returned.

### Installation

npm install jwt-validator-js

### Usage

```
const express = require('express');
const jwtValidator = require(jwt-validator-js');

const app = express();
const JWT_SECRET = 'MY_SECRET_KEY';

app.use(jwtTokenValidator({
  JWT_SECRET,
  handleErrors: {
    // your custom error code here
  },
}));

or

app.use(jwtTokenValidator({
  JWT_SECRET
}));

app.get('/', (req, res) => {
  console.log('Authenticated user:', req.userInformation);
  res.send('Hello, authenticated user!');
});

app.listen(3000, () => console.log('Server listening on port 3000'));
```

### Configuration

The middleware accepts a configuration object with the following properties:

JWT_SECRET: The secret key used for JWT verification.

handleErrors: An object with custom error messages for different scenarios.

##### The handleErrors object can contain the following properties if not specified:

1. unauthorized: The error message to return when a token is missing or invalid.
2. invalidToken: The error message to return when a token is expired or doesn't match the signing key.

---
_Source: https://npm.io/package/jwt-validator-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
