Module

admin-bro-expressjs

Plugin that allows you to add AdminBro to Express.js applications.

Installation

npm install admin-bro-expressjs

It has 2 peerDependencies: express-formidable and express, so you have to install them as well (if they are not installed already)

npm install express express-formidable

Usage

const AdminBroExpress = require('admin-bro-expressjs')

It exposes 2 methods that create an Express Router, which can be attached to a given url in the API. Each method takes a pre-configured instance of AdminBro.

If you want to use a router you have already created - not a problem. Just pass it as a predefinedRouter parameter.

You may want to use this option when you want to include some custom auth middleware for you AdminBro routes.

Example without an authentication

const AdminBro = require('admin-bro')
const AdminBroExpress = require('admin-bro-expressjs')

const express = require('express')
const app = express()

const adminBro = new AdminBro({
  databases: [],
  rootPath: '/admin',
})

const router = AdminBroExpress.buildRouter(adminBro)
app.use(adminBro.options.rootPath, router)
app.listen(8080, () => console.log('AdminBro is under localhost:8080/admin'))

Using build in authentication

To protect the routes with a session authentication, you can use predefined module:admin-bro-expressjs.buildAuthenticatedRouter method.

Note! To use authentication in production environment, there is a need to configure express-session for production build. It can be achieved by passing options to sessionOptions parameter. Read more on express/session Github page

Adding custom authentication

You can add your custom authentication setup by firstly creating the router and then passing it via the predefinedRouter option.

let router = express.Router()
router.use((req, res, next) => {
  if (req.session && req.session.admin) {
    req.session.adminUser = req.session.admin
    next()
  } else {
    res.redirect(adminBro.options.loginPath)
  }
})
router = AdminBroExpress.buildRouter(adminBro, router)

Where req.session.admin is AdminBro#CurrentAdmin, meaning that it should have at least an email property.

View Source admin-bro-expressjs/index.js, line 2

Members

# static version

Version of the plugin

View Source admin-bro-expressjs/plugin.js, line 254

Methods

# static buildAuthenticatedRouter(admin, auth, predefinedRouteropt, sessionOptionsopt, formidableOptionsopt) → {express.Router}

Builds the Express Router which is protected by a session auth

Using the router requires you to install express-session as a dependency. Normally express-session holds session in memory, which is not optimized for production usage and, in development, it causes logging out after every page refresh (if you use nodemon).

Parameters:
Name Type Attributes Default Description
admin AdminBro

instance of AdminBro

auth Object

authentication options

authenticate module:admin-bro-expressjs.Authenticate

authenticate function

cookiePassword String

secret used to encrypt cookies

cookieName String adminbro

cookie name

predefinedRouter express.Router <optional>

Express.js router

sessionOptions SessionOptions <optional>

Options that are passed to express-session

formidableOptions ExpressFormidableOptions <optional>

Options that are passed to express-session

View Source admin-bro-expressjs/plugin.js, line 141

Express.js router

express.Router
Example
const ADMIN = {
  email: '[email protected]',
  password: 'password',
}

AdminBroExpress.buildAuthenticatedRouter(adminBro, {
  authenticate: async (email, password) => {
    if (ADMIN.password === password && ADMIN.email === email) {
      return ADMIN
    }
    return null
  },
  cookieName: 'adminbro',
  cookiePassword: 'somePassword',
}, [router])

# static buildRouter(admin, predefinedRouteropt, formidableOptionsopt) → {express.Router}

Builds the Express Router that handles all the pages and assets

Parameters:
Name Type Attributes Description
admin AdminBro

instance of AdminBro

predefinedRouter express.Router <optional>

Express.js router

formidableOptions ExpressFormidableOptions <optional>

Express.js router

View Source admin-bro-expressjs/plugin.js, line 28

Express.js router

express.Router

Type Definitions

# Authenticate(emailopt, passwordopt) → {CurrentAdmin|null}

function taking 2 arguments email and password

Parameters:
Name Type Attributes Description
email string <optional>

email given in the form

password string <optional>

password given in the form

View Source admin-bro-expressjs/plugin.js, line 95

returns current admin or null

CurrentAdmin | null