redibox-hook-api

0.1.0 • Public • Published

Coverage Status Downloads npm version dependencies build License

RediBox API

This hook provides a JSON API for RediBox & any custom installed hooks. Built on Koa v2.

This is still a work in progress.

Installation

First ensure you have RediBox installed.

Install API via npm:

npm install redibox-hook-api --save

Usage

Configure api

By default your API will work out of the box, accessible at [http://127.0.0.1:1337](http://127.0.0.1:1337) with no authentication.

To override the defaults, create a new api object within your redibox config:

  • host [String]

    • default: 127.0.0.1
  • port [Number]

    • default: 1337
  • env [String]

    • default: process.env.NODE_ENV or development
  • auth [Object] See authentication.

{
  api: {
    port: 4000,
    env: 'production',
  },
}

Routes

// TODO

Authentication

By default, authentication is disabled. If enabled, the default authentication method is Basic Auth. The default username is foo & password is bar.

To configure authentication, pass the following options into the auth object in your redibox.api config:

  • enabled [Boolean]

    • default: false If false, authentication will be disabled across the entire API.
  • name [String]

    • default: foo The username for Basic Authentication.
  • pass [String]

    • default: bar The password for Basic Authentication.
  • middleware [Function]

It is possible to provide your own authentication method (e.g. OAuth 2). The middleware function provides the current hook as the only parameter, if needed. The function should return a Koa middleware compatible function.

The below example is loading a cached authentication token from Redis. This is just a basic example of how to apply asynchronous authentication middleware.

middleware(hook) {
  return async function getUser(ctx, next){
    const token = ctx.header.token;

    // Redis GET command
    return hook.client
      .get(token)
      .then(user => {
        if (!user) {
          return ctx.throw(401);
        }

        ctx.user = user;
        return next();
      });
  };
}

License

MIT

Dependents (0)

Package Sidebar

Install

npm i redibox-hook-api

Weekly Downloads

0

Version

0.1.0

License

MIT

Last publish

Collaborators

  • ehesp