# @yeldirium/telegraf-authentication-middleware

> A telegraf middleware for custom authentication.

Latest version **1.1.5** (published 2020-10-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install @yeldirium/telegraf-authentication-middleware
pnpm add @yeldirium/telegraf-authentication-middleware
yarn add @yeldirium/telegraf-authentication-middleware
bun add @yeldirium/telegraf-authentication-middleware
```

## 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.1.5 |
| Published | 2020-10-31 |
| First published | 2019-09-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 17.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Hannes Leutloff |
| Maintainers | yeldir |
| Keywords | telegraf, telegram, authentication |

## Links

- npm: https://www.npmjs.com/package/@yeldirium/telegraf-authentication-middleware
- Repository: https://github.com/yeldiRium/telegraf-authentication-middleware
- Homepage: https://github.com/yeldiRium/telegraf-authentication-middleware#readme
- Issues: https://github.com/yeldiRium/telegraf-authentication-middleware/issues
- npm.io page: https://npm.io/package/@yeldirium/telegraf-authentication-middleware

## 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.1.5 (latest) — 2020-10-31
- 1.1.4 — 2020-10-31
- 1.1.3 — 2020-05-29
- 1.1.2 — 2020-02-02
- 1.1.1 — 2020-01-15
- 1.1.0 — 2019-09-28
- 1.0.3 — 2019-09-28
- 1.0.2 — 2019-09-28
- 1.0.1 — 2019-09-28
- 1.0.0 — 2019-09-25

## README

# Telegraf Authentication Middleware

A simple authentication middleware for the [telegraf](https://github.com/telegraf/telegraf) framework.

```sh
npm install @yeldirium/telegraf-authentication-middleware
# or
yarn install @yeldirium/telegraf-authentication-middleware
```

## Status

| Category         | Status                                                                                                                                                            |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Version          | [![npm](https://img.shields.io/npm/v/@yeldirium/telegraf-authentication-middleware)](https://www.npmjs.com/package/@yeldirium/telegraf-authentication-middleware) |
| Dependencies     | ![David](https://img.shields.io/david/yeldirium/telegraf-authentication-middleware)                                                                               |
| Dev dependencies | ![David](https://img.shields.io/david/dev/yeldirium/telegraf-authentication-middleware)                                                                           |
| Build            | ![GitHub Actions](https://github.com/yeldiRium/telegraf-authentication-middleware/workflows/Release/badge.svg?branch=master)                                      |
| License          | ![GitHub](https://img.shields.io/github/license/yeldiRium/telegraf-authentication-middleware)                                                                     |

# Why this module?

If you have a telegram application and want to let your users login to a session
via `/login <token>` and logout via `/logout` and supply the authentication
method by yourself, then this is for you.
You provide a way to authenticate users based on user id and login token and you
may provider a number of handlers for various interactions during the login/logout process.

# How to use it?

```javascript
const makeAuthenticator = require("@yeldirium/telegraf-authentication-middleware");
const telegraf = require("telegraf");

// Very naive implementation of an authentication method.
const authenticator = async ({ userId, token }) => {
  const user = await database.lookupUser(userId);
  if (user.token === token) {
    return user;
  }
  return undefined;
};

const { middleware, guardMiddleware } = makeAuthenticator({ authenticator });

const bot = telegraf(token);

bot.use(middleware);

// The guardMiddleware terminates the request if no user is logged in.
bot.command("restricted", guardMiddleware, ctx => {
  console.log(ctx.user); // The currently logged in user.
});
```

You can start the `authenticator` with a map of authenticated users. This way
you can store sessions in a database and retrieve them on startup:

```javascript
const makeAuthenticator = require("@yeldirium/telegraf-authentication-middleware");
const telegraf = require("telegraf");

const authenticatedUsers = new Map();
(await database.findAuthenticatedUsers()).forEach(({ userId, userObject }) => {
  authenticatedUsers.set(userId, userObject);
});

// Very naive implementation of an authentication method.
const authenticator = async ({ userId, token }) => {
  // ...
};

const { middleware } = makeAuthenticator({ authenticator, authenticatedUsers });
```

And dump the users that currently authenticated to store them somewhere:

```javascript
// ...
const { middleware, getAuthenticatedUsers } = makeAuthenticator({
  authenticator
});

console.log(getAuthenticatedUsers()); // Map of all currently authenticated users.
```

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