# @theriot.dev/middleware

> A template for creating backend projects

Latest version **0.1.5** (published 2022-07-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @theriot.dev/middleware
pnpm add @theriot.dev/middleware
yarn add @theriot.dev/middleware
bun add @theriot.dev/middleware
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2022-07-17 |
| First published | 2022-07-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 14.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Christian Theriot |
| Maintainers | christian-theriot |
| Keywords | middleware, auth, cors, csrf, session, rate limit |

## Links

- npm: https://www.npmjs.com/package/@theriot.dev/middleware
- npm.io page: https://npm.io/package/@theriot.dev/middleware

## Dependencies (6)

- [cors](https://npm.io/package/cors.md) ^2.8.5
- [csurf](https://npm.io/package/csurf.md) ^1.11.0
- [passport](https://npm.io/package/passport.md) ^0.6.0
- [express-session](https://npm.io/package/express-session.md) ^1.17.3
- [express-rate-limit](https://npm.io/package/express-rate-limit.md) ^6.4.0
- [passport-google-one-tap](https://npm.io/package/passport-google-one-tap.md) ^1.0.0

## Alternatives

- [@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
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 0.1.5 (latest) — 2022-07-17
- 0.1.4-alpha.1.6 — 2022-07-17
- 0.1.4-alpha.1.5 — 2022-07-17
- 0.1.4-alpha.1.4 — 2022-07-17
- 0.1.4-alpha.1.3 — 2022-07-17
- 0.1.4-alpha.1.2 — 2022-07-17
- 0.1.4-alpha.1.1 — 2022-07-17
- 0.1.4-alpha.2 — 2022-07-17
- 0.1.4-alpha.1 — 2022-07-17
- 0.1.4 — 2022-07-17
- 0.1.3 — 2022-07-17
- 0.1.2 — 2022-07-17
- 0.1.1 — 2022-07-17
- 0.1.0 — 2022-07-15
- 0.0.2 — 2022-07-10
- … 1 more at https://npm.io/package/@theriot.dev/middleware/versions

## README

# @theriot.dev/middleware

This project provides a set of middleware to provide configurable functionality to an express app, including security protections, authentication, rate limiting, and internal error handling

# Installation

```
npm install express # express is a peer dependency
npm install @theriot.dev/middleware
```

# Import

```
import {
  applyOne,   # Used to apply one middleware
  applyMany,  # Used to apply an array of middleware
  auth,
  catch_errors,
  cors,
  csrf,
  rate_limit,
  session
} from '@theriot.dev/middleware'
```

# Overview

## applyOne:

Applies one piece of middleware to the express app

```
const app = express();
applyOne(app, cors());
```

## applyMany:

Applies an array of middleware to the express app

```
const app = express();
applyMany(app, [
  cors(),
  catch_errors()
]);
```

## auth

Allows routes to be protected using:

```
passport.authenticate('google-one-tap')
```

and to sign in using the callback route `/oauth2/callback`

The auth middleware has the following options:

- `serializeFn`: A function to serialize a user into the session
- `deserializeFn`: A function to deserialize a user from the session
- `clientID`: Your google client id
- `clientSecret`: Your google client secret
- `verifyFn`: A function to authenticate a user from a google profile

```
import express from 'express';
import { applyOne auth } from '@theriot.dev/middleware';

const app = express();
applyOne(app, auth({
  serializeFn: () => {...},
  deserializeFn: () => {...},
  clientID: '...',
  clientSecret: '...',
  verifyFn: () => {...}
}));
```

## catch-errors

Allows errors to be captured and a default error response to be sent

The catch_errors middleware has the following options:

- `status`: The status code to send (default is `500`)
- `message`: The message to send (default is `Internal Server Error`)

```
import express from 'express';
import { applyOne, catch_errors } from '@theriot.dev/middleware';

const app = express();
applyOne(app, catch_errors());
```

## cors

A wrapper for the [`cors`](https://www.npmjs.com/package/cors) middleware, takes only a single parameter defining a list of regex defining allowed origins

```
import express from 'express';
import { applyOne, cors } from '@theriot.dev/middleware';

const app = express();
applyOne(app, cors([
  /localhost/,
  /https?:\/\/my.domain.com(:4200)?/
]));
```

## csrf

A wrapper for the [`csurf`](https://www.npmjs.com/package/csurf) middleware, takes a single parameter defining the route for the csrf cookie generator. This must be provided as `/<your-route>:_form`, where `:_form` is required verbatim

```
import express from 'express';
import { applyOne, csrf } from '@theriot.dev/middleware';

const app = express();

// $.get('/csrfForSubmit')
// $.post('...', {_csrf, _form: 'Submit'})
applyOne(app, csrf('/csrfFor:_form'));
```

## rate-limit

A wrapper for the [`express-rate-limit`](https://www.npmjs.com/package/express-rate-limit) middleware, takes two parameters for

- `limit`: The amount of requests to allow (`100` by default)
- `windowMs`: The amount of time to create the window for, in milliseconds (`1000` by default)

```
import express from 'express';
import { applyOne, rate_limit } from '@theriot.dev/middleware';

const app = express();
applyOne(app, rate_limit());
```

## session

A wrapper for the [`express-session`](https://www.npmjs.com/package/express-session) middleware, and has the following options

- `secret`: The secret to encrypt the session
- `store`: The store implementation to use instead of the default MemoryStore
- `secure`: Whether the app is securing its cookies (https and httpOnly)

```
import express from 'express';
import { applyOne, session } from '@theriot.dev/middleware';

const app = express();
applyOne(app, session({
  secret: '...',
  store: new Store(...),
  secure: true
}));
```

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