Search…
Koa
@adminjs/koa
Make sure you have installed AdminJS packages described in Getting started article.
$ yarn add adminjs @adminjs/koa
To setup AdminJS panel with Koa you need to have koa installed and required peer dependencies:
$ yarn add koa @koa/router koa2-formidable
Afterwards, follow one of the examples below.

Simple

Javascript
Typescript
app.js
const AdminJS = require('adminjs')
const AdminJSKoa = require('@adminjs/koa')
const Koa = require('koa')
const PORT = 3000
const start = async () => {
const app = new Koa()
const admin = new AdminJS({
resources: [],
rootPath: '/admin',
})
const router = AdminJSKoa.buildRouter(admin, app)
app
.use(router.routes())
.use(router.allowedMethods())
app.listen(PORT, () => {
console.log(`AdminJS available at http://localhost:${PORT}${admin.options.rootPath}`)
})
}
start()
Now you can start your AdminJS application:
$ node app.js
Install additional dependencies:
$ yarn add -D ts-node @types/koa
app.ts
import AdminJS from 'adminjs'
import AdminJSKoa from '@adminjs/koa'
import Koa from 'koa'
const PORT = 3000
const start = async () => {
const app = new Koa()
const admin = new AdminJS({
resources: [],
rootPath: '/admin',
})
const router = AdminJSKoa.buildRouter(admin, app)
app
.use(router.routes())
.use(router.allowedMethods())
app.listen(PORT, () => {
console.log(`AdminJS available at http://localhost:${PORT}${admin.options.rootPath}`)
})
}
start()
Now you can start your AdminJS application:
$ ts-node app.ts

Authenticated

To add authentication, you must use AdminJSKoa.buildAuthenticatedRouter instead of AdminJSKoa.buildRouter.
Javascript
Typescript
app.js
const AdminJS = require('adminjs')
const AdminJSKoa = require('@adminjs/koa')
const Koa = require('koa')
const PORT = 3000
const DEFAULT_ADMIN = {
password: 'password',
}
const authenticate = async (email, password) => {
if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
return Promise.resolve(DEFAULT_ADMIN)
}
return null
}
const start = async () => {
const app = new Koa()
const admin = new AdminJS({
resources: [],
rootPath: '/admin',
})
app.keys = ['your secret for koa cookie'];
const router = AdminJSKoa.buildAuthenticatedRouter(
admin,
app,
{
authenticate,
sessionOptions: {
// You may configure your Koa session here
httpOnly: process.env.NODE_ENV === 'production',
secure: process.env.NODE_ENV === 'production',
renew: true,
},
},
)
app
.use(router.routes())
.use(router.allowedMethods())
app.listen(PORT, () => {
console.log(`AdminJS available at http://localhost:${PORT}${admin.options.rootPath}`)
})
}
start()
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.
Now you should be able to start the server and see a login form when you visit the AdminJS panel URL.
$ node app.js
Install additional dependencies:
$ yarn add -D ts-node @types/koa
app.ts
import AdminJS from 'adminjs'
import AdminJSKoa from '@adminjs/koa'
import Koa from 'koa'
const PORT = 3000
const DEFAULT_ADMIN = {
password: 'password',
}
const authenticate = async (email: string, password: string) => {
if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
return Promise.resolve(DEFAULT_ADMIN)
}
return null
}
const start = async () => {
const app = new Koa()
const admin = new AdminJS({
resources: [],
rootPath: '/admin',
})
app.keys = ['your secret for koa cookie'];
const router = AdminJSKoa.buildAuthenticatedRouter(
admin,
app,
{
authenticate,
sessionOptions: {
// You may configure your Koa session here
httpOnly: process.env.NODE_ENV === 'production',
secure: process.env.NODE_ENV === 'production',
renew: true,
},
},
)
app
.use(router.routes())
.use(router.allowedMethods())
app.listen(PORT, () => {
console.log(`AdminJS available at http://localhost:${PORT}${admin.options.rootPath}`)
})
}
start()
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.
Now you should be able to start the server and see a login form when you visit the AdminJS panel URL.
$ ts-node app.ts
Copy link
Outline
Simple
Authenticated