Huge News!Announcing our $40M Series B led by Abstract Ventures.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.1.1 to 0.2.0

2

index.js

@@ -11,3 +11,3 @@ 'use strict'

const validate = opts.validate
const validate = opts.validate.bind(fastify)
fastify.decorate('basicAuth', basicAuth)

@@ -14,0 +14,0 @@

{
"name": "fastify-basic-auth",
"version": "0.1.1",
"version": "0.2.0",
"description": "Fastify basic auth plugin",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -13,3 +13,3 @@ # 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).
This plugin decorates the fastify instance 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).

@@ -20,2 +20,3 @@ ```js

fastify.register(require('fastify-basic-auth'), { validate })
// `this` inside validate is `fastify`
function validate (username, password, req, reply, done) {

@@ -22,0 +23,0 @@ if (username === 'Tyrion' && password === 'wine') {

@@ -342,4 +342,43 @@ 'use strict'

test('Fastify context', t => {
t.plan(3)
const fastify = Fastify()
fastify.decorate('test', true)
fastify.register(basicAuth, { validate })
function validate (username, password, req, res, done) {
t.ok(this.test)
if (username === 'user' && password === 'pwd') {
done()
} else {
done(new Error('Unauthorized'))
}
}
fastify.after(() => {
fastify.route({
method: 'GET',
url: '/',
beforeHandler: fastify.basicAuth,
handler: (req, reply) => {
reply.send({ hello: 'world' })
}
})
})
fastify.inject({
url: '/',
method: 'GET',
headers: {
authorization: basicAuthHeader('user', 'pwd')
}
}, (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 200)
})
})
function basicAuthHeader (username, password) {
return 'Basic ' + Buffer.from(`${username}:${password}`).toString('base64')
}
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