# Code coverage report for api/controllers/AuthController.js

# All files / api/controllers AuthController.js

0% Statements 0/26
0% Branches 0/10
0% Functions 0/4
0% Lines 0/26

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
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104

     const RegisterUserCommand = require('../../application/auth/commands/RegisterUserCommand');
    const LoginUserCommand = require('../../application/auth/commands/LoginUserCommand');

    /**
     * Auth Controller
     * Authentication endpoints using CQRS Commands with cookie-based JWT
     */
    class AuthController {
      constructor(registerUserCommandHandler, loginUserCommandHandler, jwtService) {
        this.registerUserCommandHandler = registerUserCommandHandler;
        this.loginUserCommandHandler = loginUserCommandHandler;
        this.jwtService = jwtService;
      }

      /**
       * POST /api/auth/register - User regisztráció
       */
      async register(req, res) {
        try {
          const { name, email, password } = req.body;

          const command = new RegisterUserCommand(name, email, password);
          const result = await this.registerUserCommandHandler.handle(command);

          // Set JWT token in httpOnly cookie
          res.cookie(
            this.jwtService.getCookieName(),
            result.token,
            this.jwtService.getCookieOptions()
          );

          res.status(201).json({
            message: 'User registered successfully',
            data: {
              user: result.user
            }
          });
        } catch (error) {
          // Validációs hibák -> 400
          const status = error.message.includes('required') ||
                         error.message.includes('already exists') ||
                         error.message.includes('Invalid') ||
                         error.message.includes('must be') ? 400 : 500;

          res.status(status).json({ error: error.message });
        }
      }

      /**
       * POST /api/auth/login - User bejelentkezés
       */
      async login(req, res) {
        try {
          const { email, password } = req.body;

          const command = new LoginUserCommand(email, password);
          const result = await this.loginUserCommandHandler.handle(command);

          // Set JWT token in httpOnly cookie
          res.cookie(
            this.jwtService.getCookieName(),
            result.token,
            this.jwtService.getCookieOptions()
          );

          res.status(200).json({
            message: 'Login successful',
            data: {
              user: result.user
            }
          });
        } catch (error) {
          // Validációs vagy auth hibák -> 401
          const status = error.message.includes('Invalid') ||
                         error.message.includes('required') ? 401 : 500;

          res.status(status).json({ error: error.message });
        }
      }

      /**
       * POST /api/auth/logout - User kijelentkezés
       */
      async logout(req, res) {
        try {
          // Clear the auth cookie
          res.clearCookie(this.jwtService.getCookieName(), {
            httpOnly: true,
            secure: process.env.NODE_ENV === 'production',
            sameSite: 'strict',
            path: '/'
          });

          res.status(200).json({
            message: 'Logout successful'
          });
        } catch (error) {
          res.status(500).json({ error: error.message });
        }
      }
    }

    module.exports = AuthController;