fastify-basic-auth
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -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') { |
39
test.js
@@ -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') | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13671
367
103