1.0.2 • Published 1 year ago

authilize v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Introduction

Library for authentication middleware using JWT

Code Samples

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

List of functions

FunctionsDescription
isAuthFunction which checks if the user is authorized (retrieves bearer token from req)
isAuthPassFunction which checks if the user is authorized (retrieves bearer token from req)

List of variables

FunctionsDescriptionDefault
AUTHILIZE_JWT_SECRET_KEYrequired (only for isAuth)
AUTHILIZE_STATUS_CODEoptional401
AUTHILIZE_ERR_MESSAGEoptional"Unauthorized Access"

Installation

NPM:

npm install authilize

Yarn:

yarn add authilize