1- # fastify- basic-auth
1+ # @ fastify/ basic-auth
22
33![ CI] ( https://github.com/fastify/fastify-basic-auth/workflows/CI/badge.svg?branch=master )
4- [ ![ NPM version] ( https://img.shields.io/npm/v/fastify- basic-auth.svg?style=flat )] ( https://www.npmjs.com/package/fastify- basic-auth )
4+ [ ![ NPM version] ( https://img.shields.io/npm/v/@ fastify/ basic-auth.svg?style=flat )] ( https://www.npmjs.com/package/@ fastify/ basic-auth )
55[ ![ Known Vulnerabilities] ( https://snyk.io/test/github/fastify/fastify-basic-auth/badge.svg )] ( https://snyk.io/test/github/fastify/fastify-basic-auth )
66[ ![ js-standard-style] ( https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat )] ( https://standardjs.com/ )
77
88 A simple basic auth plugin for Fastify.
99
1010 ## Install
1111```
12- npm i fastify- basic-auth
12+ npm i @ fastify/ basic-auth
1313```
1414## Usage
15- 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 ) .
15+ 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 ) .
1616
1717``` js
1818const fastify = require (' fastify' )()
1919const authenticate = {realm: ' Westeros' }
20- fastify .register (require (' fastify- basic-auth' ), { validate, authenticate })
20+ fastify .register (require (' @ fastify/ basic-auth' ), { validate, authenticate })
2121// `this` inside validate is `fastify`
2222function validate (username , password , req , reply , done ) {
2323 if (username === ' Tyrion' && password === ' wine' ) {
@@ -40,7 +40,7 @@ Promises and *async/await* are supported as well!
4040``` js
4141const fastify = require (' fastify' )()
4242const authenticate = {realm: ' Westeros' }
43- fastify .register (require (' fastify- basic-auth' ), { validate, authenticate })
43+ fastify .register (require (' @ fastify/ basic-auth' ), { validate, authenticate })
4444async function validate (username , password , req , reply ) {
4545 if (username !== ' Tyrion' || password !== ' wine' ) {
4646 return new Error (' Winter is coming' )
@@ -52,7 +52,7 @@ Use with `onRequest`:
5252``` js
5353const fastify = require (' fastify' )()
5454const authenticate = {realm: ' Westeros' }
55- fastify .register (require (' fastify- basic-auth' ), { validate, authenticate })
55+ fastify .register (require (' @ fastify/ basic-auth' ), { validate, authenticate })
5656async function validate (username , password , req , reply ) {
5757 if (username !== ' Tyrion' || password !== ' wine' ) {
5858 return new Error (' Winter is coming' )
@@ -71,12 +71,12 @@ fastify.after(() => {
7171})
7272```
7373
74- Use with [ ` fastify- auth ` ] ( https://github.com/fastify/fastify-auth ) :
74+ Use with [ ` @ fastify/ auth` ] ( https://github.com/fastify/fastify-auth ) :
7575``` js
7676const fastify = require (' fastify' )()
7777const authenticate = {realm: ' Westeros' }
78- fastify .register (require (' fastify- auth' ))
79- fastify .register (require (' fastify- basic-auth' ), { validate, authenticate })
78+ fastify .register (require (' @ fastify/ auth' ))
79+ fastify .register (require (' @ fastify/ basic-auth' ), { validate, authenticate })
8080async function validate (username , password , req , reply ) {
8181 if (username !== ' Tyrion' || password !== ' wine' ) {
8282 return new Error (' Winter is coming' )
@@ -101,10 +101,10 @@ fastify.after(() => {
101101
102102### Custom error handler
103103
104- On failed authentication, * fastify- basic-auth* will call the Fastify
104+ On failed authentication, * @ fastify/ basic-auth * will call the Fastify
105105[ generic error
106106handler] ( https://www.fastify.io/docs/latest/Server/#seterrorhandler ) with an error.
107- * fastify- basic-auth* sets the ` err.statusCode ` property to ` 401 ` .
107+ * @ fastify/ basic-auth * sets the ` err.statusCode ` property to ` 401 ` .
108108
109109In order to properly ` 401 ` errors:
110110
@@ -144,12 +144,12 @@ This can be useful in situations where we want to trigger client-side authentica
144144As a boolean setting ` authenticate ` to ` true ` will set a header like so: ` WWW-Authenticate: Basic ` . When ` false ` , no header is added. This is the default.
145145
146146``` js
147- fastify .register (require (' fastify- basic-auth' ), {
147+ fastify .register (require (' @ fastify/ basic-auth' ), {
148148 validate,
149149 authenticate: true // WWW-Authenticate: Basic
150150})
151151
152- fastify .register (require (' fastify- basic-auth' ), {
152+ fastify .register (require (' @ fastify/ basic-auth' ), {
153153 validate,
154154 authenticate: false // no authenticate header, same as omitting authenticate option
155155})
@@ -160,7 +160,7 @@ When supplied as an object the `authenticate` option may have a `realm` key.
160160If the ` realm ` key is supplied, it will be appended to the header value:
161161
162162``` js
163- fastify .register (require (' fastify- basic-auth' ), {
163+ fastify .register (require (' @ fastify/ basic-auth' ), {
164164 validate,
165165 authenticate: {realm: ' example' } // WWW-Authenticate: Basic realm="example"
166166})
@@ -169,7 +169,7 @@ fastify.register(require('fastify-basic-auth'), {
169169The ` realm ` key could also be a function:
170170
171171``` js
172- fastify .register (require (' fastify- basic-auth' ), {
172+ fastify .register (require (' @ fastify/ basic-auth' ), {
173173 validate,
174174 authenticate: {
175175 realm (req ) {
@@ -185,7 +185,7 @@ When supplied, the header option is the name of the header to get
185185credentials from for validation.
186186
187187``` js
188- fastify .register (require (' fastify- basic-auth' ), {
188+ fastify .register (require (' @ fastify/ basic-auth' ), {
189189 validate,
190190 header: ' x-forwarded-authorization'
191191})
0 commit comments