LogoLogo
Join our community
  • AdminJS
  • Contribute
  • Demo
  • Addons Marketplace
  • Installation
    • Getting started
    • Plugins
      • Adonis
      • Express
      • Nest
      • Fastify
      • Hapi
      • Koa
      • Community Plugins
        • FeathersJS
        • AdonisJS
      • Matrix
    • Adapters
      • TypeORM
      • Sequelize
      • Prisma
      • MikroORM
      • Objection
      • SQL
      • Mongoose
      • Community Adapters
        • AdonisJS
    • What's new in v7?
    • Migration Guide v7
  • Basics
    • Resource
    • Action
    • Property
    • Features
      • Relations
      • Upload
      • Logger
      • Import & Export
      • Password
      • Leaflet Maps
      • Writing your own features
    • API
      • List
      • New
      • Search
      • Show
      • Edit
      • Delete
      • Bulk Delete
    • Themes
    • Authentication
      • FirebaseAuthProvider
      • MatrixAuthProvider
  • How to write an addon?
  • UI Customization
    • Writing your own Components
    • Overwriting CSS styles
    • Dashboard customization
    • Changing the form view
    • Storybook
  • Tutorials
    • Role-Based Access Control
    • Internationalization (i18n)
    • Content Management System
    • Custom components library
    • Custom component internationalization
  • FAQ
    • PDF Generator
    • Charts
    • Forgot Password
  • ⚠️Legacy documentation
Powered by GitBook
On this page
  • Simple
  • Authentication
  1. Installation
  2. Plugins

Hapi

@adminjs/hapi

PreviousFastifyNextKoa

Last updated 1 month ago

Make sure you have installed AdminJS packages described in article.

$ yarn add adminjs @adminjs/hapi

To setup AdminJS panel with Hapi you need to have @hapi/hapi installed and required peer dependencies:

$ yarn add @hapi/hapi @hapi/boom @hapi/cookie @hapi/inert

Afterwards, follow one of the examples below.

Simple

app.js
import AdminJSHapi from '@adminjs/hapi'
import Hapi from '@hapi/hapi'

const PORT = 3000

const start = async () => {
  const server = Hapi.server({ port: PORT })

  const adminOptions: ExtendedAdminJSOptions = {
    resources: [],
    rootPath: '/admin',
    auth: {
      isSecure: process.env.NODE_ENV === 'production',
    },
    registerInert: true,
  }

  await server.register({
    plugin: AdminJSHapi,
    options: adminOptions,
  })

  await server.start();
  console.log(`AdminJS available at ${server.info.uri}${adminOptions.rootPath}`);
}

start()
app.ts
import AdminJSHapi, { ExtendedAdminJSOptions } from '@adminjs/hapi'
import Hapi from '@hapi/hapi'

const PORT = 3000

const start = async () => {
  const server = Hapi.server({ port: PORT })

  const adminOptions: ExtendedAdminJSOptions = {
    resources: [],
    rootPath: '/admin',
    auth: {
      isSecure: process.env.NODE_ENV === 'production',
    },
    registerInert: true,
  }

  await server.register<ExtendedAdminJSOptions>({
    plugin: AdminJSHapi,
    options: adminOptions,
  })

  await server.start();
  console.log(`AdminJS available at ${server.info.uri}${adminOptions.rootPath}`);
}

start()

Authentication

To add authentication, all you have to do is extend auth config with authenticate, cookieName and cookiePassword.

app.js
import AdminJSHapi from '@adminjs/hapi'
import Hapi from '@hapi/hapi'

const PORT = 3000

const DEFAULT_ADMIN = {
  email: 'admin@example.com',
  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 server = Hapi.server({ port: PORT })

  // "secret" must be a string with at least 32 characters, example:
  const cookieSecret = 'sieL67H7GbkzJ4XCoH0IHcmO1hGBSiG5'
  const adminOptions = {
    resources: [],
    rootPath: '/admin',
    auth: {
      isSecure: process.env.NODE_ENV === 'production',
      authenticate,
      cookieName: 'adminjs',
      cookiePassword: cookieSecret,
    },
    registerInert: true,
  }

  await server.register({
    plugin: AdminJSHapi,
    options: adminOptions,
  })

  await server.start();
  console.log(`AdminJS available at ${server.info.uri}${adminOptions.rootPath}`);
}

start()
app.ts
import AdminJSHapi, { ExtendedAdminJSOptions } from '@adminjs/hapi'
import Hapi from '@hapi/hapi'

const PORT = 3000

const DEFAULT_ADMIN = {
  email: 'admin@example.com',
  password: 'password',
}

const authenticate = async (email: string, password: string): Promise<Record<string, unknown> | null> => {
  if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
    return Promise.resolve(DEFAULT_ADMIN)
  }
  return null
}

const start = async () => {
  const server = Hapi.server({ port: PORT })

  // "secret" must be a string with at least 32 characters, example:
  const cookieSecret = 'sieL67H7GbkzJ4XCoH0IHcmO1hGBSiG5'
  const adminOptions: ExtendedAdminJSOptions = {
    resources: [],
    rootPath: '/admin',
    auth: {
      isSecure: process.env.NODE_ENV === 'production',
      authenticate,
      cookieName: 'adminjs',
      cookiePassword: cookieSecret,
    },
    registerInert: true,
  }

  await server.register<ExtendedAdminJSOptions>({
    plugin: AdminJSHapi,
    options: adminOptions,
  })

  await server.start();
  console.log(`AdminJS available at ${server.info.uri}${adminOptions.rootPath}`);
}

start()
Getting started
Page cover image