Library for authentication middleware using JWT
Initialize Authilize instance
const router = require("express").Router();
const { postController } = require("../controllers/controller");
const { isAuth, isAuthPass } = require("Authilize");
// POST
router.post("/", isAuth, postController);
router.post("/", isAuthPass, postController);
module.exports = router;
Controller example
exports.postController = async (req, res, next) => {
const { username, email } = req.decodedData // Get your data from req.decodedData.
return res.status(200).json({ message: "successful" });
};
You can customize it as you like
if not then the status code will be 401 by default and error message will be "Unauthorized Access"
.env (VARIABLES MUST BE THE SAME)
AUTHILIZE_JWT_SECRET_KEY = Fdjjfklsdjfklasf
AUTHILIZE_STATUS_CODE = 401
AUTHILIZE_ERR_MESSAGE = Unauthorized Access
If you are using isAuthPass and the token is invalid or not passed in the headers then req.decodedData will be undefined
Functions | Description |
---|---|
isAuth |
Function which checks if the user is authorized (retrieves bearer token from req) |
isAuthPass |
Function which checks if the user is authorized (retrieves bearer token from req) |
Functions | Description | Default |
---|---|---|
AUTHILIZE_JWT_SECRET_KEY |
required (only for isAuth) | |
AUTHILIZE_STATUS_CODE |
optional | 401 |
AUTHILIZE_ERR_MESSAGE |
optional | "Unauthorized Access" |
NPM:
npm install authilize
Yarn:
yarn add authilize