New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fastify-basic-auth

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-basic-auth - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

.travis.yml

27

package.json
{
"name": "fastify-basic-auth",
"version": "0.0.1",
"description": "",
"version": "0.1.0",
"description": "Fastify basic auth plugin",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "standard && tap test.js"
},

@@ -13,3 +13,9 @@ "repository": {

},
"keywords": [],
"keywords": [
"fastify",
"basic",
"auth",
"authentication",
"plugin"
],
"author": "Tomas Della Vedova - @delvedor (http://delved.org)",

@@ -20,3 +26,14 @@ "license": "MIT",

},
"homepage": "https://github.com/fastify/fastify-basic-auth#readme"
"homepage": "https://github.com/fastify/fastify-basic-auth#readme",
"devDependencies": {
"fastify": "^1.5.0",
"fastify-auth": "^0.2.0",
"standard": "^11.0.1",
"tap": "^12.0.1"
},
"dependencies": {
"basic-auth": "^2.0.0",
"fast-json-stringify": "^1.5.3",
"fastify-plugin": "^1.0.1"
}
}

@@ -1,1 +0,101 @@

# fastify-basic-auth
# fastify-basic-auth
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
[![Build Status](https://travis-ci.org/fastify/fastify-basic-auth.svg?branch=master)](https://travis-ci.org/fastify/fastify-basic-auth)
A simple basic auth plugin for Fastify.
## Install
```
npm i fastify-basic-auth
```
## Usage
This plugin decorates the fastifyinstance with a `basicAuth` function, which you can use inside a `preHandler` hook, in a `beforeHander` or with [`fastify-auth`](https://github.com/fastify/fastify-auth).
```js
const fastify = require('fastify')()
fastify.register(require('fastify-basic-auth'), { validate })
function validate (username, password, req, reply, done) {
if (username === 'Tyrion' && password === 'wine') {
done()
} else {
done(new Error('Winter is coming'))
}
}
fastify.after(() => {
fastify.addHook('preHandler', fastify.basicAuth)
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
})
```
Promises and *async/await* are supported as well!
```js
const fastify = require('fastify')()
fastify.register(require('fastify-basic-auth'), { validate })
async function validate (username, password, req, reply) {
if (username !== 'Tyrion' || password !== 'wine') {
return new Error('Winter is coming')
}
}
```
Use with `beforeHander`:
```js
const fastify = require('fastify')()
fastify.register(require('fastify-basic-auth'), { validate, disableHook: true })
async function validate (username, password, req, reply) {
if (username !== 'Tyrion' || password !== 'wine') {
return new Error('Winter is coming')
}
}
fastify.after(() => {
fastify.route({
method: 'GET',
url: '/',
beforeHandler: fastify.basicAuth,
handler: async (req, reply) => {
return { hello: 'world' }
}
})
})
```
Use with [`fastify-auth`](https://github.com/fastify/fastify-auth):
```js
const fastify = require('fastify')()
fastify.register(require('fastify-auth'))
fastify.register(require('fastify-basic-auth'), { validate, disableHook: true })
async function validate (username, password, req, reply) {
if (username !== 'Tyrion' || password !== 'wine') {
return new Error('Winter is coming')
}
}
fastify.after(() => {
// use preHandler to authenticate all the routes
fastify.addHook('preHandler', fastify.auth([fastify.basicAuth]))
fastify.route({
method: 'GET',
url: '/',
// use beforeHanderto authenticatejust this one
beforeHandler: fastify.auth([fastify.basicAuth]),
handler: async (req, reply) => {
return { hello: 'world' }
}
})
})
```
## License
Licensed under [MIT](./LICENSE).
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc