# @apikee/authenticator-common

> Common classes for @apikee/authenticator

Latest version **0.0.7** (published 2022-07-17) · 0 weekly downloads

## Install

```sh
npm install @apikee/authenticator-common
pnpm add @apikee/authenticator-common
yarn add @apikee/authenticator-common
bun add @apikee/authenticator-common
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.7 |
| Published | 2022-07-17 |
| First published | 2022-07-16 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jakub Tomáš |
| Maintainers | apikee |

## Links

- npm: https://www.npmjs.com/package/@apikee/authenticator-common
- Repository: https://github.com/apikee/authenticator-common
- Homepage: https://github.com/apikee/authenticator-common#readme
- Issues: https://github.com/apikee/authenticator-common/issues
- npm.io page: https://npm.io/package/@apikee/authenticator-common

## Recent versions

- 0.0.7 (latest) — 2022-07-17
- 0.0.6 — 2022-07-17
- 0.0.5 — 2022-07-16
- 0.0.4 — 2022-07-16
- 0.0.3 — 2022-07-16
- 0.0.2 — 2022-07-16
- 0.0.1 — 2022-07-16

## README

## @apikee/authenticator-common

**Common classes for @apikee/authenticator. This package can be used when creating custom Store for [@apikee/authenticator](https://github.com/apikee/authenticator)**

Example of custom store and required methods:

```tsx
import { Store } from "@apikee/authenticator-common";
import { JwtPayload } from "jsonwebtoken";
import mongoose, { ConnectOptions } from "mongoose";
import { RecordModel } from "./models";

export class CustomStore extends Store {
  constructor(
    private _connectionString: string,
    private _connectionOptions: ConnectOptions = {},
    private _connectionCallback?: () => void
  ) {
    super();
    this._connect();
  }

  private _connect = async () => {
    // connect to DB
  };

  addToken = async (
    token: string,
    subject: string,
    replace: boolean = false
  ) => {
    try {
      if (replace) {
        await RecordModel.deleteMany({ subject });
      }

      await RecordModel.create({ _id: token, subject });
    } catch (error) {
      console.log("Cannot addToken", error);
    }
  };

  findSubjectByToken = async (token: string) => {
    try {
      const record = await RecordModel.findOne({ _id: token });
      return record?.subject as string;
    } catch (error) {
      console.log("Cannot findSubjectByToken", error);
      return "";
    }
  };

  deleteToken = async (token: string) => {
    try {
      await RecordModel.deleteOne({ _id: token });
    } catch (error) {
      console.log("Cannot deleteToken", error);
    }
  };

  deleteAllTokensForSubject = async (subject: string) => {
    try {
      await RecordModel.deleteMany({ subject });
    } catch (error) {
      console.log("Cannot deleteAllTokensForSubject", error);
    }
  };

  clearExpiredTokens = async (
    validateToken: (
      type: "access" | "refresh",
      token: string
    ) => JwtPayload | string | null
  ) => {
    try {
      const records = await RecordModel.find();

      await Promise.all(
        records.map(async ({ _id: token }) => {
          if (!validateToken("refresh", token)) {
            await this.deleteToken(token);
          }
        })
      );
    } catch (error) {
      console.log("Cannot clearExpiredTokens", error);
    }
  };
}

```

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