# elf-authentication

> Elf Authentication is a set of classes, methods and decorators to aid with authentication when using TypeORM and Express.

Latest version **1.2.0** (published 2020-01-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install elf-authentication
pnpm add elf-authentication
yarn add elf-authentication
bun add elf-authentication
```

## 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.2.0 |
| Published | 2020-01-09 |
| First published | 2019-08-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 38.9 KB |
| Known vulnerabilities | 0 (+4 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Christian Dréy |
| Maintainers | christiandrey |

## Links

- npm: https://www.npmjs.com/package/elf-authentication
- Repository: https://github.com/christiandrey/authentication
- Homepage: https://github.com/christiandrey/authentication#readme
- Issues: https://github.com/christiandrey/authentication/issues
- npm.io page: https://npm.io/package/elf-authentication

## Dependencies (5)

- [passport](https://npm.io/package/passport.md) ^0.4.0
- [elf-utils](https://npm.io/package/elf-utils.md) 1.1.x
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1
- [passport-jwt](https://npm.io/package/passport-jwt.md) ^4.0.0
- [passport-local](https://npm.io/package/passport-local.md) ^1.0.0

## Recent versions

- 1.2.0 (latest) — 2020-01-09
- 1.1.3-beta — 2019-12-04
- 1.1.2-beta — 2019-11-02
- 1.1.1-beta — 2019-11-02
- 1.1.0-beta — 2019-11-02
- 1.0.9-alpha — 2019-10-01
- 1.0.8-alpha — 2019-10-01
- 1.0.7-alpha — 2019-10-01
- 1.0.6-alpha — 2019-09-28
- 1.0.5-alpha — 2019-09-28
- 1.0.4-alpha — 2019-09-28
- 1.0.3-alpha — 2019-08-24
- 1.0.2-alpha — 2019-08-08
- 1.0.1-alpha — 2019-08-08
- 1.0.0-alpha — 2019-08-08

## README

# Elf Authentication

Elf Authentication is a set of classes, methods and decorators to aid with authentication when using TypeORM and Express.

## Classes

---

It exports a base `ElfUser` model class, which can be extended in the target project to include more properties related to the user, and a base `ElfRole` class, for adding user roles.

### ElfUser

---

> The `ElfUser`class contains the following properties:

- `id`
- `username`
- `email`
- `emailVerified`
- `emailVerificationToken`
- `passwordResetToken`
- `passwordHash`
- `isDisabled`

### ElfRole

---

> The `ElfRole` class contains the following properties:

- `id`
- `name`
- `permissions`

## Decorators

---

Elf Authentication provides the following decorators for use in controllers.

### @AuthorizeRoutes()

---

> Decorate a controller class with this to apply authorization to all methods within that controller.

### @Authorize()

---

> Decorate a class method with this to apply authorization to just that method.

### @AllowAnonymous()

---

> Use this decorator to exempt a method from inherited authorization, such as one initiated with the `AuthorizeRoutes` decorator.

The `AuthorizeRoutes` and `Authorize` decorators accept options as an optional parameter. Set the `checkPermissions` flag to `true` to also authorize user permissions.

## Methods

---

Elf Authentication makes available the following helper methods.

### InitializeAuthentication

---

> Use this to initialize authentication. Provide as first parameter, a reference to the TypeORM User Repository e.g. `InitializeAuthentication(getRepository(User))`. This is typically done before once a DB Connection has been made.

### AuthenticateUser

---

> Use this asynchronous method to authenticate a user. It's typically used at login, and accepts the express `request` and `response` objects. It returns a `TokenResponse` which contains `token`, `expiresIn` and `tokenType` e.g. `const token = await AuthenticateUser(req, resp)`

### CreateUserSession

---

> Use this asynchronous method to create a user session from a user. It's typically used after creating a user and accepts the `request` object and a user object. It also returns a `TokenResponse`. e.g. `const token = await CreateUserSession(req, createdUser)`

## Customising

---

The following environment variables are available to use to customise the authorization process:
| Tables | Are |
| ------------- |-------------|
| **ELF_AUTH_CIPHER_KEY** | Use this to define an encryption key for authorization. |
| **ELF_AUTH_EXPIRES_IN** | Use this to customise how long the generated access token is valid for. The default value is `7 days`. |

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