Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/udamir/adminjs-fastify
Fastify plugin for AdminJS
https://github.com/udamir/adminjs-fastify
admin admin-panel adminjs fastify-plugin
Last synced: about 2 months ago
JSON representation
Fastify plugin for AdminJS
- Host: GitHub
- URL: https://github.com/udamir/adminjs-fastify
- Owner: udamir
- License: mit
- Created: 2021-09-26T22:14:18.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-04-13T22:49:00.000Z (over 2 years ago)
- Last Synced: 2024-11-10T19:39:42.035Z (about 2 months ago)
- Topics: admin, admin-panel, adminjs, fastify-plugin
- Language: TypeScript
- Homepage:
- Size: 76.2 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fastify plugin for AdminJS
This is a plugin which integrates [AdminJS](https://github.com/SoftwareBrothers/adminjs) to [fastify](https://github.com/fastify/fastify/) framework.
## AdminJS
AdminJS is an automatic admin interface which can be plugged into your application. You, as a developer, provide database models (like posts, comments, stores, products or whatever else your application uses), and AdminJS generates UI which allows you (or other trusted users) to manage content.
Check out the example application with mongo and postgres models here: https://adminjs-example-app-staging.herokuapp.com/admin
Or visit [AdminJS](https://github.com/SoftwareBrothers/adminjs) github page.
# Usage
## Installation
```sh
npm install adminjs-fastify
```## Import plugin
```ts
import { adminRoute } from "adminjs-fastify"
```It exposes `adminjs` plugin, which can be registered to a given url in the API.
## Example without an authentication
```ts
import { adminRoute } from "adminjs-fastify"
import AdminJS from 'adminjs'
import fastify from "fastify"const app = fastify()
const adminJs = new AdminJS({
databases: [],
rootPath: '/admin' // prefix for adminJS routes
})app.register(adminRoute, { admin: adminJs })
app.listen(8080, () => console.log('AdminJS is running under localhost:8080/admin'))
```Check example project sources in `examples` folder.
## Using build in authentication
To protect the routes with a session authentication, you can use `auth` parameter to plugin
```ts
app.register(adminRoute, {
admin: adminJs,
auth: {
authenticate: async (email, password) => {
if (ADMIN.password === password && ADMIN.email === email) {
return ADMIN
}
return null
},
cookiePassword: "a secret with minimum length of 32 characters",
}
})
```Note! To use authentication in production environment, there is a need to configure @fastify/session for production build. It can be achieved by passing options to `sessionOptions` parameter. Read more on [fastify/session Github page](https://github.com/fastify/session)
## Plugin options documentation
Plugin can be pre-configured with following options:
```ts
type AdminRouterOptions = {
/**
* instance of {@link AdminJS}
*/
admin: AdminJS/**
* authentication parameters
*/
auth?: {
/**
* cookie secret - minimum length of 32 characters
*/
cookiePassword: string/**
* cookie name
* @default: "adminJS"
*/
cookieName?: string/**
* secure cookie
* @default: false
*/
cookieSecure?: boolean/**
* authentication check function
* @return: user data or null
*/
authenticate: (email: string, password: string) => unknown | null
}/**
* multipart options - pass through params to fastify-multipart plugin
*/
multipartOptions?: FastifyMultipartBaseOptions/**
* session options - pass through params to @fastify/session plugin
*/
sessionOptions?: FastifySessionPlugin.Options
})
```# License
MIT