# @leonardosarmentocastro/authentication

> authentication

Latest version **2.4.5** (published 2024-06-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install @leonardosarmentocastro/authentication
pnpm add @leonardosarmentocastro/authentication
yarn add @leonardosarmentocastro/authentication
bun add @leonardosarmentocastro/authentication
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.5 |
| Published | 2024-06-24 |
| First published | 2021-05-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=14.16 |
| Dependencies | 10 |
| Unpacked size | 30.7 KB |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| Author | leonardosarmentocastro |
| Keywords | authentication, express, mongoose |

## Links

- npm: https://www.npmjs.com/package/@leonardosarmentocastro/authentication
- npm.io page: https://npm.io/package/@leonardosarmentocastro/authentication

## Dependencies (10)

- [dayjs](https://npm.io/package/dayjs.md) ^1.10.4
- [upash](https://npm.io/package/upash.md) ^1.0.2
- [mongoose](https://npm.io/package/mongoose.md) 6.5.0
- [@phc/pbkdf2](https://npm.io/package/@phc/pbkdf2.md) ^1.1.14
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^9.0.0
- [@leonardosarmentocastro/crud](https://npm.io/package/@leonardosarmentocastro/crud.md) ^2.4.5
- [@leonardosarmentocastro/i18n](https://npm.io/package/@leonardosarmentocastro/i18n.md) ^2.0.0
- [@leonardosarmentocastro/server](https://npm.io/package/@leonardosarmentocastro/server.md) ^2.0.0
- [@leonardosarmentocastro/database](https://npm.io/package/@leonardosarmentocastro/database.md) ^2.4.5
- [@leonardosarmentocastro/validate](https://npm.io/package/@leonardosarmentocastro/validate.md) ^2.1.3

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 2.4.5 (latest) — 2024-06-24
- 2.4.4 — 2024-06-24
- 2.4.3 — 2024-06-24
- 2.4.2 — 2024-06-24
- 2.4.1 — 2024-06-22
- 2.4.0 — 2024-06-17
- 2.3.1 — 2024-06-17
- 2.3.0 — 2024-06-17
- 2.2.1 — 2024-04-15
- 2.2.0 — 2024-04-15
- 2.1.3 — 2024-02-12
- 2.1.2 — 2023-12-26
- 2.1.1 — 2023-12-10
- 2.1.0 — 2023-12-10
- 2.0.0 — 2023-12-07
- … 39 more at https://npm.io/package/@leonardosarmentocastro/authentication/versions

## README

# authentication

authentication using express mongoose

## usage

```js
const mongoose = require('mongoose');
const server = require('@leonardosarmentocastro/server');
const i18n = require('@leonardosarmentocastro/i18n'); // mandatory
const { authentication, authenticationSchema } = require('@leonardosarmentocastro/authentication');

(async () => {
  const api = await server.start(8080, {
    middlewares: (app) => {
      i18n.connect(app); // mandatory
    },
    routes: (app) => {
      const schema = new mongoose.Schema({ authentication: authenticationSchema, name: String });
      const model = new mongoose.model('Customer', schema);

      // creates
      // * "[GET] /authentication/me"
      // * "[POST] /authentication/sign-in"
      // * "[POST] /authentication/sign-out"
      // * "[POST] /authentication/sign-up"
      authentication.connect(app, model);

      // creates
      // * "[POST] /authentication/2FA/cancel"
      // * "[POST] /authentication/2FA/check"
      // * "[POST] /authentication/2FA/verify"
      authentication.twoFactor.connect(app, model);
    },
  });
})();
```

## exposed routes

For authentication

- `[GET] /authentication/me`: serves authenticated model data;
- `[POST] /authentication/sign-in`: validates token + authenticates an model, saving a JWT Authorization token to the model and attaching it to response's header;
- `[POST] /authentication/sign-up`: creates/authenticates an model, saving a JWT Authorization token to the model and attaching it to response's header;
- `[POST] /authentication/sign-out`: validates token + remove JWT Authorization token from model and remove it from response's header.

For 2FA verification

- `[POST] /authentication/2FA/cancel`: cancel the verification request before the 5 minute duration runs out;
- `[POST] /authentication/2FA/check`: checks if the served 4 digit PIN sent by the user, matches the one sent to the given cellphone number;
- `[POST] /authentication/2FA/verify`: sends a SMS with a 4 digit PIN code to a given cellphone number.

## environment variables

* `AUTHENTICATION_SECRET`: secret used to sign jwt tokens when signing tokens;
* `AUTHENTICATION_SMS_2FA_VONAGE_API_KEY`: API key for sending 2FA SMS using `Vonage`;
* `AUTHENTICATION_SMS_2FA_VONAGE_API_SECRET`: API secret for sending 2FA SMS using `Vonage`;
* `AUTHENTICATION_SMS_2FA_SENDER_NAME`: The sender name that is going to be written on 2FA SMS messages.

## translation keys

For schema validation:

* `AUTHENTICATION_VALIDATOR_ERROR_INVALID_CELLPHONE_NUMBER`
* `VALIDATOR_ERROR_PASSWORD_NOT_STRONG`
* `VALIDATOR_ERROR_FIELD_IS_REQUIRED`

For endpoint resolvers:

* `AUTHENTICATION_ERROR_CELLPHONE_NUMBER_NOT_FOUND`
* `AUTHENTICATION_ERROR_PASSWORD_MISMATCH`
* `AUTHENTICATION_ERROR_REGISTRY_FOR_TOKEN_NOT_FOUND`
* `AUTHENTICATION_ERROR_TOKEN_EXPIRED`
* `AUTHENTICATION_ERROR_TOKEN_INVALID`
* `AUTHENTICATION_ERROR_TOKEN_NOT_BEFORE`

For cellphone 2FA through SMS using `Vonage`:

* `AUTHENTICATION_SMS_2FA_CANCEL_UNEXPECTED_ERROR`
* `AUTHENTICATION_SMS_2FA_CHECK_UNEXPECTED_ERROR`
* `AUTHENTICATION_SMS_2FA_CELLPHONE_NUMBER_ALREADY_REGISTERED`
* `AUTHENTICATION_SMS_2FA_VERIFICATION_UNEXPECTED_ERROR`

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