text: const User = require("../models/User");
  const CryptoJS = require("crypto-js");
  const jwt = require("jsonwebtoken");
  
  const register = async (req, res) => {
      const newUser = new User({
          username: req.body.username,
          email:req.body.email,
          password: CryptoJS.AES.encrypt(req.body.password, process.env.PASS_SEC).toString() //TODO can be added to UserSchema.pre at User.js
      })
  
      try{
          const savedUser = await newUser.save()
          res.status(201).json(savedUser)             // successful
      }catch (err){
          console.log(err);
          res.status(500).json(err)
      }
  }
  
  const login = async (req, res) => {
      try {
          const user = await User.findOne({username:req.body.username})
          !user && res.status(401).json("wrong credentials!!!")       // if there isn't user
  
          const hashedPassword = CryptoJS.AES.decrypt(user.password, process.env.PASS_SEC)
          const originalpassword = hashedPassword.toString(CryptoJS.enc.Utf8);
  
          originalpassword !== req.body.password && res.status(401).json("wrong credentials!!!!")
  
          const accessToken = jwt.sign({id:user._id, isAdmin:user.isAdmin},process.env.JWT_SEC,{expiresIn: "3d"})
          // send user_id and isAdmin as JWT
          const {password, ...others} = user._doc
          res.status(200).json({...others, accessToken})
      }catch (err) {
          console.log(err);
      }
  }
  
  module.exports = {register, login}