1
0
Fork 0
nodejs-backend-template/server/middlewares/unpack-access-token.js

23 lines
807 B
JavaScript

const ApplicationError = require('../utils/application-error');
/**
* Middleware for checking for and decrypting authorization tokens.
*
* This middleware checks for and decrypts the authorization token passed in the request header.
* If the token is missing or has an invalid format, an error is generated.
*
* @param {Object} req - Express request object.
* @param {Object} res - The Express response object.
* @param {function} next - A function to move to the next middleware.
* @throws {ApplicationError} Error if the token is missing or has an invalid format.
*/
module.exports = async (req, res, next) => {
const header = req.headers['authorization'];
if (!header || !header.includes(' ')) {
throw ApplicationError.NoAuthHeader();
}
req.accessToken = header.split(' ')[1];
next();
};