## Installation
npm install @adminjs/hapi @hapi/boom @hapi/inert
Plugin depends on the following packages and they have to be installed beforehand:
If you want to use built-in auth, you'll also need this:
Usage
The plugin can be registered using standard server.register
method.
The simplest example:
const AdminJSPlugin = require('@adminjs/hapi')
const Hapi = require('@hapi/hapi')
const adminJsOptions = {
resources: [YourResource],
}
const server = Hapi.server({ port: process.env.PORT || 8080 })
const start = async () => {
await server.register({
plugin: AdminJSPlugin,
options: adminJsOptions,
})
await server.start()
}
start()
The example above will launch the admin panel under default localhost:8080/admin
url.
Routes will be accessible by all users without any authentication.
To restrict access, you can pass auth
via plugin options.
Authentication options
Plugin receives all AdminJSOptions and one special parameter: auth
, which controls the authentication.
- By default, if you won't give
options.auth
- admin panel will be available without the authentication (like in the simplest example above) - You can set whatever authentication you prefer for admin routes by setting
options.auth.strategy
. For example:
//...
await server.register(require('hapi-auth-basic'))
server.auth.strategy('simple', 'basic', { validate })
await server.register({
plugin: AdminJSPlugin,
options: { auth: { strategy: 'simple' }, ...otherAdminJSOptions },
})
//...
The strategy will be passed down to all AdminJS routes.
- @adminjs/hapi plugin can be setup to use auth-cookie. Only thing you have to do is to define the following auth options: authenticate, cookiePassword, isSecure, cookieName.
Methods
# async static register()
Actual method that Hapi uses under the hood when you call
server.register(plugin, options) method. Options you give in Hapi are passed back to it.
Example
const AdminJSPlugin = require('@adminjs/hapi')
const Hapi = require('@hapi/hapi')
// see AdminJS documentation on database setup.
const yourDatabase = require('your-database-setup-file')
const ADMIN = {
email: 'text@example.com',
password: 'password',
}
const adminJsOptions = {
resources: [yourDatabase],
auth: {
authenticate: (email, password) => {
if (ADMIN.email === email && ADMIN.password === password) {
return ADMIN
}
return null
},
strategy: 'session',
cookieName: 'adminJsCookie',
cookiePassword: process.env.COOKIE_PASSWORD || 'makesurepasswordissecure',
isSecure: true, //only https requests
},
}
const server = Hapi.server({ port: process.env.PORT || 8080 })
const start = async () => {
await server.register({
plugin: AdminJSPlugin,
options: adminJsOptions,
})
await server.start()
}
start()