https://github.com/giovannicardamone/fastify-autosecurity
fastest way to map files to security in fastify
https://github.com/giovannicardamone/fastify-autosecurity
Last synced: 2 months ago
JSON representation
fastest way to map files to security in fastify
- Host: GitHub
- URL: https://github.com/giovannicardamone/fastify-autosecurity
- Owner: GiovanniCardamone
- License: mit
- Created: 2021-08-08T21:41:39.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-05-22T16:50:24.000Z (almost 3 years ago)
- Last Synced: 2025-02-19T22:38:39.682Z (2 months ago)
- Language: TypeScript
- Size: 235 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-autosecurity

 
[](https://www.npmjs.com/package/fastify-autosecurity)
[](https://www.npmjs.com/package/fastify-autosecurity)
[](https://snyk.io/test/github/GiovanniCardamone/fastify-autosecurity)
[](https://github.com/GiovanniCardamone/fastify-autosecurity/blob/master/LICENSE)
[](https://coveralls.io/github/GiovanniCardamone/fastify-autosecurity?branch=master)> :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](https://giovannicardamone.github.io/fastify-autosecurity/)**
[fastify-autosecurity](https://giovannicardamone.github.io/fastify-autosecurity/)
## :rocket: Install
```sh
npm install --save fastify-autosecurity
```## :blue_book: Usage
### Register plugin
```js
const fastify = require('fastify')
const server = fastify()server.register(require('fastify-autosecurity'), {
dir: './', // relative to your cwd
})
```### Create file in autosecurity directory
```js
//file: `/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
```typescript
//file: `/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 => ({
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](./LICENSE)
## :sparkles: Contributors
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification.
Contributions of any kind welcome!