# @mjakubec/jwt-verify

> Express.js middleware to handle JWT authorization via Authorization header.

Latest version **1.0.1** (published 2021-09-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install @mjakubec/jwt-verify
pnpm add @mjakubec/jwt-verify
yarn add @mjakubec/jwt-verify
bun add @mjakubec/jwt-verify
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-09-24 |
| First published | 2021-09-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 2.3 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Martin Jakubec |
| Maintainers | mjakubec |
| Keywords | express, jwt, authorization |

## Links

- npm: https://www.npmjs.com/package/@mjakubec/jwt-verify
- Repository: https://github.com/martinjakubec/jwt-verify-middleware
- Issues: https://github.com/martinjakubec/jwt-verify-middleware/issues
- npm.io page: https://npm.io/package/@mjakubec/jwt-verify

## Dependencies (1)

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

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2021-09-24
- 1.0.0 — 2021-09-24

## README

# @mjakubec/jwt-verify

Simple express middleware to authorize JSON Web Tokens and authorize users.

Provides `isUserLoggedIn` property (`true` or `false`) on `res.locals` object.

Furthermore, it decodes the values in JWT and provides them as `tokenValues` property on `res.locals` object so that you can see contents of the token.

## Usage

Let's say that the JWT contains `role` value which can be set to either `admin` or `user`

```js
const express = require('express');
const app = express();
const JWTVerify = require('@mjakubec/jwt-verify');

app.use('*', JWTVerify('super_secret_string_for_JWT'));

app.get('/', (req, res) => {
  if (res.locals.isUserLoggedIn) {
    if (res.locals.tokenValues.role === 'admin') { 
      res.send('Hey there, admin!')
    } else if (res.locals.tokenValues.role === 'user' ) {
      res.send('Hello, logged in user.')
    }
  } else {
    res.send('Begone, you heathen.')
  }
})

app.listen(3000);
```

In this case, if the user provides a valid JWT, they can access the restricted content which firther differentiates between user roles.

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