Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 0 additions & 208 deletions auth.js

This file was deleted.

28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const fp = require('fastify-plugin')
const createAuthHandler = require('./lib/auth-handler')
const { ALLOWED_RELATIONS, DEFAULT_RELATION } = require('./lib/constants')

/**
* @type {typeof import('./types/index').fastifyAuth}
*/
function fastifyAuth (fastify, opts, next) {
const specifiedRelation = opts.defaultRelation
if (specifiedRelation && !ALLOWED_RELATIONS.includes(specifiedRelation)) {
const error = new Error(`The value of default relation should be one of [${ALLOWED_RELATIONS}], not "${specifiedRelation}"`)
return next(error)
}
const pluginOptions = {
defaultRelation: specifiedRelation || DEFAULT_RELATION
}
fastify.decorate('auth', createAuthHandler(pluginOptions))
next()
}

module.exports = fp(fastifyAuth, {
fastify: '5.x',
name: '@fastify/auth'
})
module.exports.default = fastifyAuth
module.exports.fastifyAuth = fastifyAuth
87 changes: 87 additions & 0 deletions lib/auth-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use strict'

const reusify = require('reusify')
const AuthState = require('./auth-state')
const { ALLOWED_RELATIONS } = require('./constants')

/**
* @param { import('../types/index').FastifyAuthPluginOptions } pluginOptions
* @returns {(
* handlers: import('../types/index').FastifyAuthHandlerArray,
* authOptions?: import('../types/index').FastifyAuthOptions
* ) => (request: import('fastify').FastifyRequest, reply: import('fastify').FastifyReply, done: (error?: Error) => void) => void}
*/
function createAuthHandler (pluginOptions) {
return function (handlers, authOptions) {
if (!Array.isArray(handlers)) {
throw new TypeError(`You must give an array of functions, not "${typeof handlers}"`)
}
if (!handlers.length) {
throw new Error('Missing auth functions')
}
handlers.forEach((fn, i) => {
if (!Array.isArray(fn)) {
handlers[i] = fn.bind(this)
return
}
fn.forEach((nestedFn, j) => {
if (Array.isArray(nestedFn)) {
throw new TypeError('Nesting sub-arrays is not supported')
}
fn[j] = nestedFn.bind(this)
})
})
const instance = reusify(AuthState)
const options = resolveAuthOptions(pluginOptions, authOptions)
return createRequestHandler(instance, handlers, options)
}
}

/**
* @param {import('../types/index').FastifyAuthPluginOptions} pluginOptions
* @param {import('../types/index').FastifyAuthOptions} authOptions
* @returns {Required<import('../types/index').FastifyAuthOptions>}
*/
function resolveAuthOptions (pluginOptions, authOptions) {
const resolvedOptions = {
relation: pluginOptions.defaultRelation,
run: null,
...authOptions
}
const { relation, run } = resolvedOptions
if (!ALLOWED_RELATIONS.includes(relation)) {
throw new Error(`The value of default relation should be one of [${ALLOWED_RELATIONS}], not "${relation}"`)
}
if (run && run !== 'all') {
throw new Error(`The value of run option must be "all", not "${run}"`)
}
return resolvedOptions
}

/**
* @param {{ get(): import('./auth-state'), release(obj: import('./auth-state')): void }} instance
* @param {import('../types/index').FastifyAuthHandlerArray} handlers
* @param {Required<import('../types/index').FastifyAuthOptions>} options
* @returns {(request: import('fastify').FastifyRequest, reply: import('fastify').FastifyReply, done: (error?: Error) => void) => void}
*/
function createRequestHandler (instance, handlers, options) {
return (request, reply, done) => {
const state = instance.get()
state.i = 0
state.j = 0
state.handlers = handlers
state.options = options
state.request = request
state.reply = reply
state.done = done
state.currentError = null
state.skipFurtherErrors = false
state.skipFurtherArrayErrors = false
state.onComplete = () => instance.release(state)
state.nextAuthHandler()
}
}

module.exports = createAuthHandler
module.exports.resolveAuthOptions = resolveAuthOptions
module.exports.createRequestHandler = createRequestHandler
Loading