Express
@adminjs/express
To setup AdminJS panel with Express.js you need to have express installed and required peer dependencies:
$ yarn add express tslib express-formidable express-sessionAfterwards, follow one of the examples below.
Simple
import AdminJS from 'adminjs'
import AdminJSExpress from '@adminjs/express'
import express from 'express'
const PORT = 3000
const start = async () => {
const app = express()
const admin = new AdminJS({})
const adminRouter = AdminJSExpress.buildRouter(admin)
app.use(admin.options.rootPath, adminRouter)
app.listen(PORT, () => {
console.log(`AdminJS started on http://localhost:${PORT}${admin.options.rootPath}`)
})
}
start()Install Express types:
$ yarn add -D @types/expressimport AdminJS from 'adminjs'
import AdminJSExpress from '@adminjs/express'
import express from 'express'
const PORT = 3000
const start = async () => {
const app = express()
const admin = new AdminJS({})
const adminRouter = AdminJSExpress.buildRouter(admin)
app.use(admin.options.rootPath, adminRouter)
app.listen(PORT, () => {
console.log(`AdminJS started on http://localhost:${PORT}${admin.options.rootPath}`)
})
}
start()Now you can start your AdminJS application.
Authenticated
To add authentication, you must use AdminJSExpress.buildAuthenticatedRouter instead of AdminJSExpress.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.
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.
Last updated
