fastify-autosecurity
v0.7.4
Published
fastest way to map directories to URLs in fastify
Downloads
63
Keywords
Readme
fastify-autosecurity
:star: Thanks to everyone who has starred the project, it means a lot!
plugin to handle securities in fastify automatically based on file name.
:newspaper: Full Documentation
:rocket: Install
npm install --save fastify-autosecurity
:blue_book: Usage
Register plugin
const fastify = require('fastify')
const server = fastify()
server.register(require('fastify-autosecurity'), {
dir: './<autosecurity-directory>', // relative to your cwd
})
Create file in autosecurity directory
//file: `<autosecurity-directory>/some/route.js`
//url: `http://your-host/some/route`
export default (fastifyInstance) => ({
security: {
type: 'basic,
},
handle: async (token) => {
return { user: 'my user id from token', whatever: 'something else i got from my application', scopes: ['user-basic', 'admin'] }
},
validScopes: ['user-basic', 'manager', 'consultant', 'admin'],
scopes: async (myUser, scopesOfRote) => {
return true // your logic to check scopes of user against scopes required in route
}
})
Using typescript support for module
//file: `<autosecurity-directory>/some/route.ts`
//url: `http://your-host/some/route`
import { FastifyInstance } from 'fastify'
interface MyUser {
user: string
whatever: string
scopes: string[]
}
export default (fastify: FastifyInstance): StrictBasicAuthSecurity<MyUser> => ({
security: {
type: 'basic,
},
handle: async (token: string) => {
return { user: 'my user id from token', whatever: 'something else i got from my application', scopes: ['user-basic', 'admin'] }
},
validScopes: ['user-basic', 'manager', 'consultant', 'admin'],
scopes: async (myUser: MyUser, scopesOfRote: string[]) => {
return true // your logic to check scopes of user against scopes required in route
}
})
:page_facing_up: License
Licensed under MIT
:sparkles: Contributors
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification.
Contributions of any kind welcome!