DPDK Architecture  ->  Bootstrap (v3.0.5)

User
>= v1.17.1

Simple user middleware for Express
Opens up several api endpoints to post to for handling user login and registration.

Features

  • Includes the default function with configuration from the application config

Bootstrap

Config

No config implemented

Implementation

const user = require('./middleware/user');

this.server.use(user(this.config.user));

Usage

Config

{
  "user": {
    "enabled": false,
    "algorithm": "HS512",
    "secret": "p3sp8kCBeAXuby9Syexqsx9tZEGKUVPdW9uZ6tXwHcd8LuWWmryB9DYmKZWzpHgLuFyUnjPBQE8L82rgrCHYAL45vpPa9Ng",
    "expiresIn": "24h"
  }
}

Endpoints

* (POST) /api/user/register        => executes bootstrap.config.user.register(req, success, failed);
* (POST) /api/user/activate        => executes bootstrap.config.user.activate(req, success, failed);
* (POST) /api/user/login           => executes bootstrap.config.user.login(req, success, failed);
* (POST) /api/user/forgot-password => executes bootstrap.config.user.forgotPassword(req, success, failed);
* (POST) /api/user                 => executes bootstrap.config.user.validate(valid);

Custom login handler

/**
 * Login is executed whenever you post to <code>/api/user/login</code>. Pass the required user data in the
 * success function, which is then tokenized using JSON Web tokens (JWT) and passed to the frontend.
 * req is passed for the developer to get the POST body and validate the login.
 */
bootstrap.config.user.login = (req, success, failed) => {
  // Handle login by developer
  const user = { username: 'user.name@dpdk.com' };
  if (user) return success(1011, { user });

  return failed(5012);
};

Custom register handler

/**
 * Register is executed whenever you post to <code>/api/user/register</code>. The request body can be
 * used by the developer to check credentials etc, and eventually return the success() or failed() function.
 * Using these function standardizes the result passed to the frontend.
 */
bootstrap.config.user.register = (req, success, failed) => {
  // Handle registration by developer...
  const registered = registerAnUser();
  if (registered) return success(1011, { userDataForTheFrontend: { username: 'user.name@dpdk.com' }});

  return failed(5030);
};

Custom activate handler

/**
 * Activate is executed whenever you post to <code>/api/user/activate</code>. The request body can be
 * used by the developer to activate an account based on the activation key, and eventually return the success() or failed() function.
 * Using these function standarizes the result passed to the frontend.
 */
bootstrap.config.user.activate = (req, success, failed) => {
  // Handle activation by developer...
  const activated = activateAnUser();
  if (activated) return success(1010, { userDataForTheFrontend: { username: 'user.name@dpdk.com' }});

  return failed(5030);
};

Custom validate handler

/**
 * Validate is executed whenever you post to <code>/api/user</code>. The token from the headers (req.headers.jwt)
 * or the token in the body (req.body.token) will be used to validate the given token.
 */
bootstrap.config.user.validate = valid => {
  // Not really a useful function, but the user token can be checked.
  // Valid: true/false
};

Custom forgot password handler

/**
 * Forgot password is executed whenever you post to <code>/api/user/forgot-password</code>.
 */
bootstrap.config.user.forgotPassword = (req, success, failed) => {
  // Can be used to send a new activation mail
};

Links

No links available

Last modifiedFriday, April 30, 2021, 12:00:21 PM UTC
Last authorColin van Eenige
Commit ID4c7a701