fastify-basic-auth
Advanced tools
Comparing version 0.5.0 to 1.0.0
@@ -1,27 +0,32 @@ | ||
import fastify = require('fastify'); | ||
import { | ||
FastifyRequest, | ||
FastifyPlugin, | ||
FastifyReply, | ||
onRequestHookHandler, | ||
preParsingHookHandler, | ||
preValidationHookHandler, | ||
preHandlerHookHandler | ||
} from 'fastify' | ||
import { Server, IncomingMessage, ServerResponse } from 'http'; | ||
declare module 'fastify' { | ||
interface FastifyInstance<HttpServer, HttpRequest, HttpResponse> { | ||
basicAuth: FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>; | ||
interface FastifyInstance { | ||
basicAuth: onRequestHookHandler | | ||
preParsingHookHandler | | ||
preValidationHookHandler | | ||
preHandlerHookHandler | ||
} | ||
} | ||
declare const fastifyBasicAuth: fastify.Plugin< | ||
Server, | ||
IncomingMessage, | ||
ServerResponse, | ||
{ | ||
validate: ( | ||
username: string, | ||
password: string, | ||
req: fastify.FastifyRequest, | ||
reply: fastify.FastifyReply<ServerResponse>, | ||
done: (err?: Error) => void | ||
) => void; | ||
authenticate?: boolean | { realm: string }; | ||
} | ||
>; | ||
export interface FastifyBasicAuthOptions { | ||
validate( | ||
username: string, | ||
password: string, | ||
req: FastifyRequest, | ||
reply: FastifyReply, | ||
done: (err?: Error) => void | ||
): void | Promise<void>; | ||
authenticate?: boolean | { realm: string }; | ||
} | ||
export = fastifyBasicAuth; | ||
declare const fastifyBasicAuth: FastifyPlugin<FastifyBasicAuthOptions> | ||
export default fastifyBasicAuth; |
@@ -67,4 +67,4 @@ 'use strict' | ||
module.exports = fp(basicPlugin, { | ||
fastify: '>=1.0.0', | ||
fastify: '3.x', | ||
name: 'fastify-basic-auth' | ||
}) |
{ | ||
"name": "fastify-basic-auth", | ||
"version": "0.5.0", | ||
"version": "1.0.0", | ||
"description": "Fastify basic auth plugin", | ||
@@ -8,4 +8,3 @@ "main": "index.js", | ||
"scripts": { | ||
"typescript": "tsc --project ./test/types/tsconfig.json", | ||
"test": "standard && tap test/*.test.js && npm run typescript" | ||
"test": "standard && tap test.js && tsd" | ||
}, | ||
@@ -30,13 +29,16 @@ "repository": { | ||
"devDependencies": { | ||
"@types/node": "^12.7.12", | ||
"fastify": "^2.0.0", | ||
"fastify-auth": "^0.3.0", | ||
"standard": "^11.0.1", | ||
"tap": "^12.0.1" | ||
"fastify": "^3.0.0-rc.1", | ||
"fastify-auth": "^1.0.0", | ||
"standard": "^14.3.3", | ||
"tap": "^14.10.7", | ||
"tsd": "^0.11.0" | ||
}, | ||
"dependencies": { | ||
"basic-auth": "^2.0.0", | ||
"fastify-plugin": "^1.0.1", | ||
"http-errors": "^1.7.2" | ||
"basic-auth": "^2.0.1", | ||
"fastify-plugin": "^2.0.0", | ||
"http-errors": "^1.7.3" | ||
}, | ||
"engines": { | ||
"node": ">=10.0.0" | ||
} | ||
} |
# fastify-basic-auth | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) | ||
[![Build Status](https://dev.azure.com/fastify/fastify/_apis/build/status/fastify.fastify-basic-auth?branchName=master)](https://dev.azure.com/fastify/fastify/_build/latest?definitionId=7&branchName=master) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) ![Node CI](https://github.com/fastify/fastify-basic-auth/workflows/Node%20CI/badge.svg) | ||
@@ -13,3 +12,3 @@ A simple basic auth plugin for Fastify. | ||
## Usage | ||
This plugin decorates the fastify instance with a `basicAuth` function, which you can use inside a `preHandler` hook, in a `beforeHandler` or with [`fastify-auth`](https://github.com/fastify/fastify-auth). | ||
This plugin decorates the fastify instance with a `basicAuth` function, which you can use inside any hook before your route handler, or with [`fastify-auth`](https://github.com/fastify/fastify-auth). | ||
@@ -30,3 +29,3 @@ ```js | ||
fastify.after(() => { | ||
fastify.addHook('preHandler', fastify.basicAuth) | ||
fastify.addHook('onRequest', fastify.basicAuth) | ||
@@ -51,3 +50,3 @@ fastify.get('/', (req, reply) => { | ||
Use with `preHandler`: | ||
Use with `onRequest`: | ||
```js | ||
@@ -67,3 +66,3 @@ const fastify = require('fastify')() | ||
url: '/', | ||
preHandler: fastify.basicAuth, | ||
onRequest: fastify.basicAuth, | ||
handler: async (req, reply) => { | ||
@@ -95,4 +94,4 @@ return { hello: 'world' } | ||
url: '/', | ||
// use beforeHanderto authenticatejust this one | ||
preHandler: fastify.auth([fastify.basicAuth]), | ||
// use onRequest to authenticate just this one | ||
onRequest: fastify.auth([fastify.basicAuth]), | ||
handler: async (req, reply) => { | ||
@@ -129,8 +128,8 @@ return { hello: 'world' } | ||
The `validate` function is called on each request made, | ||
and is passed the `username`, `password`, `req` and `reply` | ||
The `validate` function is called on each request made, | ||
and is passed the `username`, `password`, `req` and `reply` | ||
parameters in that order. An optional fifth parameter, `done` may be | ||
used to signify a valid request when called with no arguments, | ||
or an invalid request when called with an `Error` object. Alternatively, | ||
the `validate` function may return a promise, resolving for valid | ||
used to signify a valid request when called with no arguments, | ||
or an invalid request when called with an `Error` object. Alternatively, | ||
the `validate` function may return a promise, resolving for valid | ||
requests and rejecting for invalid. This can also be achieved using | ||
@@ -143,3 +142,3 @@ an `async/await` function, and throwing for invalid requests. | ||
When supplied, the `authenticate` option will cause the | ||
When supplied, the `authenticate` option will cause the | ||
[`WWW-Authenticate` header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate) to be added. It may also be used to set the `realm` value. | ||
@@ -152,9 +151,9 @@ | ||
```js | ||
fastify.register(require('fastify-basic-auth'), { | ||
validate, | ||
fastify.register(require('fastify-basic-auth'), { | ||
validate, | ||
authenticate: true // WWW-Authenticate: Basic | ||
}) | ||
fastify.register(require('fastify-basic-auth'), { | ||
validate, | ||
fastify.register(require('fastify-basic-auth'), { | ||
validate, | ||
authenticate: false // no authenticate header, same as omitting authenticate option | ||
@@ -169,4 +168,4 @@ }) | ||
```js | ||
fastify.register(require('fastify-basic-auth'), { | ||
validate, | ||
fastify.register(require('fastify-basic-auth'), { | ||
validate, | ||
authenticate: {realm: 'example'} // WWW-Authenticate: Basic realm="example" | ||
@@ -173,0 +172,0 @@ }) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
23286
0
170
+ Addedfastify-plugin@2.3.4(transitive)
+ Addedsemver@7.7.1(transitive)
- Removedfastify-plugin@1.6.1(transitive)
- Removedsemver@6.3.1(transitive)
Updatedbasic-auth@^2.0.1
Updatedfastify-plugin@^2.0.0
Updatedhttp-errors@^1.7.3