aboutsummaryrefslogtreecommitdiff
path: root/pages/api/get-user.js
blob: 36bc974e73ed1bfb54bd2f19b91e5a45a809265a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import clientPromise from "../../lib/mongodb";

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

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

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

  return user;
}

export default async function handler(req, res) {
  const { userName } = req.query;
  const user = await getUser(userName);

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