diff options
| author | Factiven <[email protected]> | 2023-04-13 16:03:57 +0700 |
|---|---|---|
| committer | Factiven <[email protected]> | 2023-04-13 16:03:57 +0700 |
| commit | b365d89a11adf40d37b78292f121b890e960d0e8 (patch) | |
| tree | 6bd745c773dc48a2e5e4c18d2f71d54d82d682fd /pages/api | |
| parent | update 1 (diff) | |
| download | moopa-b365d89a11adf40d37b78292f121b890e960d0e8.tar.xz moopa-b365d89a11adf40d37b78292f121b890e960d0e8.zip | |
update 2nd
Diffstat (limited to 'pages/api')
| -rw-r--r-- | pages/api/getUser.js | 20 | ||||
| -rw-r--r-- | pages/api/update-user.js | 21 |
2 files changed, 41 insertions, 0 deletions
diff --git a/pages/api/getUser.js b/pages/api/getUser.js new file mode 100644 index 0000000..7df10a6 --- /dev/null +++ b/pages/api/getUser.js @@ -0,0 +1,20 @@ +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 }); + + 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); +} diff --git a/pages/api/update-user.js b/pages/api/update-user.js new file mode 100644 index 0000000..9f652ac --- /dev/null +++ b/pages/api/update-user.js @@ -0,0 +1,21 @@ +// pages/api/update-user.js +import clientPromise from "../../lib/mongodb"; + +export default async function handler(req, res) { + const client = await clientPromise; + const db = client.db("authbase"); + const collection = db.collection("users"); + + const { name, newData } = req.body; // id is the user ID and newData is the new data you want to set + + try { + const result = await collection.updateOne( + { name: name }, + { $set: newData } + ); + + res.status(200).json(result); + } catch (error) { + res.status(500).json({ error: "Unable to update user data" }); + } +} |