text: const bcrypt = require("bcryptjs");
  const jwt = require("jsonwebtoken");
  const { response } = require("./utils");
  
  const SECRET = process.env.SECRET;
  
  module.exports = {
    async register(req, res) {
      try {
        const { User } = req.context.models;
        
        const userAlreadyExists = await User.findOne({
          username: req.body.username,
        });
  
        if (userAlreadyExists) response(res, false, 400, "User already exists.");
  
        req.body.password = await bcrypt.hash(req.body.password, 10);
        const user = await User.create(req.body);
  
        response(res, true, 201, "", user);
      } catch (error) {
        response(res, false, 500, "", error);
      }
    },
    async login(req, res) {
      try {
        const { User } = req.context.models;
  
        const user = await User.findOne({
          username: req.body.username,
        });
  
        if (!user) response(res, false, 404, "User doesn't exist.");
  
        const passwordResult = await bcrypt.compare(
          req.body.password,
          user.password
        );
  
        if (!passwordResult) response(res, false, 401, "Password doesn't match.");
  
        const token = await jwt.sign({ username: user.username }, SECRET);
  
        response(res, true, 200, "", token);
      } catch (error) {
        response(res, false, 500, "", error);
      }
    },
  };
  