# Code coverage report for api/middlewares/authMiddleware.js

# All files / api/middlewares authMiddleware.js

0% Statements 0/11
0% Branches 0/2
0% Functions 0/1
0% Lines 0/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45

     const JwtService = require('../../application/services/JwtService');

    const jwtService = new JwtService();

    /**
     * Authentication Middleware - JWT token ellenőrzés (Cookie-based)
     *
     * Ezt a middleware-t használd protected route-okon!
     *
     * Példa használat:
     * router.get('/me', authMiddleware, userController.getMe);
     */
    function authMiddleware(req, res, next) {
      try {
        // 1. Token kinyerése cookieból
        const token = jwtService.extractTokenFromCookies(req.cookies);

        if (!token) {
          return res.status(401).json({
            error: 'Authentication required',
            message: 'No token provided in cookies'
          });
        }

        // 2. Token verifikálása
        const decoded = jwtService.verifyToken(token);

        // 3. User adatok elhelyezése req.user-ben (controller-ek használhatják)
        req.user = {
          userId: decoded.userId,
          email: decoded.email
        };

        // 4. Folytatás
        next();
      } catch (error) {
        return res.status(401).json({
          error: 'Authentication failed',
          message: error.message
        });
      }
    }

    module.exports = authMiddleware;