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

Fastify basic auth plugin

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3K
increased by6.34%
Maintainers
1
Weekly downloads
 
Created
Source

fastify-basic-auth

js-standard-style Build Status

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.

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!

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:

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:

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.

Keywords

FAQs

Package last updated on 29 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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