# fastify-auth-scheme

> Fastify Authentication Library

Latest version **0.1.2** (published 2020-07-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install fastify-auth-scheme
pnpm add fastify-auth-scheme
yarn add fastify-auth-scheme
bun add fastify-auth-scheme
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2020-07-16 |
| First published | 2020-07-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 25.9 KB |
| Known vulnerabilities | 0 (+4 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Vinicius Teixeira |
| Maintainers | vinicius0026 |
| Keywords | node, fastify, auth |

## Links

- npm: https://www.npmjs.com/package/fastify-auth-scheme
- Repository: https://github.com/vinicius0026/fastify-auth-scheme
- Homepage: https://github.com/vinicius0026/fastify-auth-scheme#readme
- Issues: https://github.com/vinicius0026/fastify-auth-scheme/issues
- npm.io page: https://npm.io/package/fastify-auth-scheme

## Dependencies (2)

- [fastify](https://npm.io/package/fastify.md) ^3.0.2
- [fastify-plugin](https://npm.io/package/fastify-plugin.md) ^2.0.1

## 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

- 0.1.2 (latest) — 2020-07-16
- 0.1.1 — 2020-07-16
- 0.1.0 — 2020-07-16

## README

# fastify-auth-scheme

Inspired by hapi authenticatio flow, this plugin brings allows to register authentication strageties in your fastify server, define a default strategy that will be applied to all routes and allows overriding the default at the route.

This plugin doesn't implement any authentication strategies though, that's up to you to define.

## Usage

To use `fastify-auth-scheme` you need to register it in your fastify instance and add some auth strategies, like so:

```typescript
import fastify from "fastify";
import fastifyAuthScheme from "fastify-auth-scheme";

const server = fastify();
server.register(fastifyAuthScheme);

server.auth.addStrategy("my-auth-strategy", async (request, reply) => {
  // do your auth logic here
  // if authenticated properly, return { isValid: true, credentials: userCreds }
  // otherwise return { isValid: false }
  return { isValid: false };
});

// register default strategy for all routes
server.auth.setDefault("my-auth-strategy");

// register your routes
server.get("/", async (request, reply) => {
  // only accessible for authenticated users
  // the returned credentials will be available in request.auth.credentials
  return `hello ${request.auth.credentials}`;
});

// route level configuration of auth - this route will no require authentication
server.get("/no-auth", { config: { auth: false } }, async (request, reply) => {
  return "no auth";
});
```

## Auth Strategy API

To register a new strategy, you need to give it a unique name and a function to handle the authentication.

The authentication function must take in the `request` and `reply` objects from fastify and return

```typescript
interface AuthStrategyReturn {
  isValid: boolean; // whether the authentication succeeded
  credentials?: any; // the credentials for the authenticated user
}
```

After you register a strategy, you can make it default by calling `server.auth.setDefault(<auth name>)`. This will apply the default auth strategy to all routes that don't have an override config.

## Route level configuration

You can define route-level authentication by passing a config object to the route, like so:

```typescript
server.get("/", { config: { auth: AuthConfig } }, routeHandler);
```

The AuthConfig is one of:

- `false`: this disables authentication for this route, preventing default authentication strategy from being applied
- `"try"`: this option will still run the default strategy (if registered) and will set the user credentials in the request if authentication succeeds, but it will not prevent access to the route if authentication fails (in which case the request.auth will be null).
- `<strategy name>`: by passing a strategy name to `auth` config, you can override the default auth scheme and use a specific one to a given route.

If nothing is passed to auth config, the default auth will be used.

## LICENSE

MIT License

Copyright (C) 2020 Vinicius Teixeira <vinicius0026@gmail.com>

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