aboutsummaryrefslogtreecommitdiff
path: root/pages/api/get-user.js
blob: 94ec845ed16dd88cac500e0a18e19b45e8429d16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import clientPromise from "../../lib/mongodb";

export async function getUser(userId) {
  const client = await clientPromise;
  const db = client.db("authbase");

  const collection = db.collection("users");
  const user = await collection.findOne({ id: userId });

  if (user && user._id) {
    user._id = String(user._id);
  }

  return user;
}

export default async function handler(req, res) {
  const { userId } = req.query;

  if (!userId) {
    return res.status(400).json({ message: "User ID is required" });
  }

  const user = await getUser(userId);

  if (!user) {
    return res.status(404).json({ message: "User not found" });
  }

  res.status(200).json(user);
}