Files
StevenandGitHub 4466ba436b chore(examples): use default prettier for examples/templates (#60530)
## Description
This PR ensures that the default prettier config is used for examples
and templates.

This config is compatible with `prettier@3` as well (upgrading prettier
is bigger change that can be a future PR).

## Changes
- Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will
be needed upgrading to prettier@3)
- Added `examples/.prettierrc.json` with default config (this will
change every example)
- Added `packages/create-next-app/templates/.prettierrc.json` with
default config (this will change every template)

## Related

- Fixes #54402
- Closes #54409
2024-01-11 16:01:44 -07:00

42 lines
1.3 KiB
JavaScript

import nextConnect from "next-connect";
import auth from "../../middleware/auth";
import { deleteUser, createUser, updateUserByUsername } from "../../lib/db";
const handler = nextConnect();
handler
.use(auth)
.get((req, res) => {
// You do not generally want to return the whole user object
// because it may contain sensitive fields such as !!password!! Only return what is needed
// const { name, username, favoriteColor } = req.user
// res.json({ user: { name, username, favoriteColor } })
res.json({ user: req.user });
})
.post((req, res) => {
const { username, password, name } = req.body;
createUser(req, { username, password, name });
res.status(200).json({ success: true, message: "created new user" });
})
.use((req, res, next) => {
// handlers after this (PUT, DELETE) all require an authenticated user
// This middleware to check if user is authenticated before continuing
if (!req.user) {
res.status(401).send("unauthenticated");
} else {
next();
}
})
.put((req, res) => {
const { name } = req.body;
const user = updateUserByUsername(req, req.user.username, { name });
res.json({ user });
})
.delete((req, res) => {
deleteUser(req);
req.logOut();
res.status(204).end();
});
export default handler;