const logger = require("../utils/logger");
const jwt = require("jsonwebtoken");

const cannySsoHandler = async (req, res) => {
  try {
    const userData = {
      //avatarURL: user.avatarURL, // optional, but preferred
      email: req.user.email,
      id: req.user.uid,
      name: req.user.displayName || req.user.email
    };
    return res.status(200).send(jwt.sign(userData, process.env.CANNY_PRIVATE_KEY, { algorithm: "HS256" }));
  } catch (error) {
    logger.log("sso-canny-error", "error", req?.user?.email, null, {
      message: error.message,
      stack: error.stack
    });
    res.status(500).json({ error: error.message });
  }
};

module.exports = {
  cannySsoHandler
};
