Make sure you have installed AdminJS packages described in Getting started article.
$yarnaddadminjs@adminjs/fastify
To setup AdminJS panel with Fastify you need to have fastify installed and required peer dependencies:
$yarnaddfastifytslib
Afterwards, follow one of the examples below.
Simple
app.js
import AdminJSFastify from'@adminjs/fastify'import AdminJS from'adminjs'import Fastify from'fastify'constPORT=3000conststart=async()=>{constapp=Fastify()constadmin=newAdminJS({ databases: [], rootPath:'/admin'})awaitAdminJSFastify.buildRouter( admin, app, )app.listen({ port:PORT},(err,addr)=>{if (err) {console.error(err)}else{console.log(`AdminJS started on http://localhost:${PORT}${admin.options.rootPath}`)}})}start()
Authentication
To add authentication, you must use AdminJSFastify.buildAuthenticatedRouter instead of AdminJSFastify.buildRouter. Additionally, we must set up a session store to keep our session information. In the example below we will store our session in a Postgres table, we will also use connect-pg-simple to allow our session store to connect to the database.
Install additional dependencies:
As you may have noticed, the authenticate function compares credentials you submit in the form with a hardcoded DEFAULT_ADMIN object. In your case, you might want to modify authenticate function's logic to compare form credentials against real database objects.
If you plan to copy-paste this example, make sure you set the database connection string to a functional one.
Install additional dependencies:
As you may have noticed, the authenticate function compares credentials you submit in the form with a hardcoded DEFAULT_ADMIN object. In your case, you might want to modify authenticate function's logic to compare form credentials against real database objects.
If you plan to copy-paste this example, make sure you set the database connection string to a functional one.