auth server middleware
this is developed to validate the request and get the user data from the jwt token with express.js
getting the accessToken from the cookie. used the cookie-parser and JWT for the validation and user Data.
$ npm install auth-server-cookie
var express =var authServer =var secret_access = processenvSECRET_ACCESSvar app =app
Create a new cookie parser middleware function.
secreta string used for validating the JWT token. This is required and if not specified, will raise an error.
The middleware will parse the
Cookie header on the request and expose the
cookie data as the property
make sure that you are storing the cookie in the client side or passing it in the cookie header and the token in the
var Router = ;var UsersController = ;var authServer =const router = ;router;;
This is the router middleware to validate user request with jwt token and get the user data by parsing the jwt token.
The middleware will parse the accessToken from the cookie and validate it by using the JWT and the provided secret.
also it will parse data from the token and populate the data to the request. then we can access it in the controller.
cookie parse with cookie-parser.
token parse and verification with JWT jsonwebtoken