Skip to content

Instantly share code, notes, and snippets.

@delvedor
Last active July 19, 2017 19:53
Show Gist options
  • Save delvedor/4874e35dc8db49c0e0b2e47db47f1a71 to your computer and use it in GitHub Desktop.
Save delvedor/4874e35dc8db49c0e0b2e47db47f1a71 to your computer and use it in GitHub Desktop.
'use strict'
const fp = require('fastify-plugin')
const reusify = require('reusify')
function checkAuth (fastify, opts, next) {
fastify.decorate('auth', auth)
next()
}
function auth (functions) {
if (!Array.isArray(functions)) {
throw new Error('You must give an array of functions to the auth function')
}
if (!functions.length) {
throw new Error('Missing auth functions')
}
for (var i = 0; i < functions.length; i++) {
functions[i] = functions[i].bind(this)
}
var instance = reusify(Auth)
function _auth (request, reply, done) {
var obj = instance.get()
obj.request = request
obj.reply = reply
obj.done = done
obj.functions = this.functions
obj.i = 0
obj.nextAuth()
}
return _auth.bind({ functions })
function Auth () {
this.next = null
this.i = 0
this.functions = []
this.request = null
this.reply = null
this.done = null
var that = this
this.nextAuth = function nextAuth (err) {
var func = that.functions[that.i++]
if (!func) {
if (!that.reply.res.statusCode || that.reply.res.statusCode < 400) {
that.reply.code(401)
}
instance.release(that)
that.done(err)
return
}
func(that.request, that.reply, that.onAuth)
}
this.onAuth = function onAuth (err) {
if (err) {
return that.nextAuth(err)
}
instance.release(that)
return that.done()
}
}
}
module.exports = fp(checkAuth, '>=0.13.1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment