1.0.2 • Published 3 years ago

validation-middleware-test v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

validation-middleware-test

validateAccessToken middleware will validate token from request query. If token.query does not exist or doesn't match the provided token, response with status 403 will be sent. Alternatively, you can provide a callback function that will be executed when validation fails.

How to use

run npm i validation-middleware-test

After installed, can be used as following:

import express from "express";
import { validateAccessToken } from "validation-middleware-test";
import yourController from "../controllers/yourController";
const router = express.Router();

const TOKEN = "your_token";

// yourController.loadSomething won't be called if query.token is incorrect or missing
router.get("/", validateAccessToken(TOKEN), yourController.loadSomething)

export default router;

Using with callback

You can pass a callback for custom behavior on failing.

By default when validation failed, the following code will be executed:

res.status(403).json({ error: 'Forbidden' });

For custom behavior pass a callback function with req, res, next params:

import express from "express";
import { validateAccessToken } from "validation-middleware-test";
import yourController from "../controllers/yourController";
const router = express.Router();

const TOKEN = "your_token";

// yourController.loadSomething won't be called if query.token is incorrect or missing
router.get(
  "/",
  validateAccessToken(token, (req, res, next) =>
    // change default behavior on validation failure
    // you can send a different response, or maybe throw an error here
    res.status(400).json("Invalid token")
  ),
  yourController.loadSomething
);

export default router;