pesto-students/batch-11-relayer

View on GitHub
server/middlewares/authentication.js

Summary

Maintainability
A
0 mins
Test Coverage
import tokenLib from '../lib/authTokenLib';
import responseLib from '../lib/responseLib';
import * as actionStatus from '../constants/actionStatus';

const validateAuthentication = async (req, res, next) => {
  try {
    const decoded = await tokenLib.getAuthCookie(req);
    req.userId = decoded.userId;
    next();
  } catch (e) {
    const generatedResponse = responseLib.generateResponse(
      true,
      'Authentication Error',
      actionStatus.NOT_ALLOWED,
      null,
    );
    res.clearCookie('authToken')
    res.status(401).send(generatedResponse);
  }
};

export default validateAuthentication;