@atptalos/common v1.0.26
Commons Module
The purpose of this module is to share code between services
Middlewares
Requirement
cookie-session middleware must be used in your Express application
import cookieSession from "cookie-session";
import express from "express";
const app = express();
app.use(
cookieSession({
signed: false,
secure: process.env.NODE_ENV !== "test", //set cookies when request commes from https
})
);
This is required since current-user
middleware will validate the request contains a session
property, that was included by the cookieSession
middleware.
Usage
current-user
will validate the jwt
inside session
property. If the validation succeed, the request object will receive a currentUser
property with the user data
interface UserPayload {
id: string;
name: string;
email: string;
}
Then the request will continue its way until it hits your route.
Use it in your express application by applying it as middleware
import { currentUser } from "@atptalos/common";
app.use(noRequireAuth)
app.use(currentUser) // Routes applied below currentUser will require the user to be authenticated
app.use(routerRequireAuth)
Validate if user data exist in the request object. If it doesn't exist. We immediately return NotAuthorizedError
Requirement
current-user
middleware in your app
Example
import { validateAuthorization } from "@atptalos/common";
const router = express.Router();
router.get("/api/your/endpoint", validateAuthorization, async (req, res) => {
// your route logic here
// you can access safely req.currentUser
})
Validate if express validator thrown any error while validating body parameters
Requirement
express-validator
middleware must be implemented on your routes.
import { validateRequest } from "@atptalos/common";
import { body } from "express-validator";
const router = express.Router();
router.post(
"/api/your/endpoint",
[
body("name").not().isEmpty().withMessage("Name is required"),
body("email").isEmail().withMessage("Email must be valid"),
],
validateRequest, async (req, res) => {
// your route logic here
})
you can access safely to req.body properties since express-validator applied some validations and validateRequest middleware ensures no error was found. If any error was found during the validation, validateRequest will throw RequestValidationError
🚀 Deployment
Automated deployments in @atptalos/common
Everytime your changes in this module get merged in master, deploy-common.yaml
workflow will publish a new version in the public @atptalos
organizacion
10 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago