import { Request, Response } from "express";
import User from "../../models/user";

// Returns the logged-in user (from the JWT), fresh from the database, so
// clients don't have to rely on the snapshot they cached at login.
const getProfile = async (req: Request, res: Response) => {
  try {
    const user = await User.findById(req.user?._id);
    if (!user) {
      return res.status(404).json({ message: "User not found" });
    }
    res.status(200).json({ user });
  } catch (err: any) {
    res.status(500).json({ message: err?.message || "Something went wrong" });
  }
};

export default getProfile;
