const jwt = require("jsonwebtoken");

const loginHandler = (req, res) => {
  const { username, password } = req.body;
  const user = username==process.env.USER_NAME && password==process.env.PASSWORD

  if (user) {
    const token = jwt.sign({ username: user.username }, process.env.ACCESS_TOKEN_SECRET, {
      expiresIn: '1d' // expires in one day
    });
    res.json({ token });
  } else {
    res.status(400).send('Invalid Credentials');
  }
};

module.exports = { loginHandler };
