Skip to content

Instantly share code, notes, and snippets.

@jwoyo
Last active December 10, 2019 08:21
Show Gist options
  • Save jwoyo/3d8c84faeeded1d4ce0b2ee96513cc21 to your computer and use it in GitHub Desktop.
Save jwoyo/3d8c84faeeded1d4ce0b2ee96513cc21 to your computer and use it in GitHub Desktop.
const restaurantOwnerOnlyMiddleware = async (req, res, next) => {
const {restaurantId} = req.params;
const user = req.user;
const restaurantDoc = await db.collection("restaurants").doc(restaurantId).get(); // db-read
const {owner} = restaurantDoc.data();
if (owner !== req.user.uid) {
res.status(403).send("Unauthorized");
return;
}
next();
};
app.put("/:restaurantId/stars", [onlyLoggedInUsers, restaurantOwnerOnlyMiddleware], (req, res) => {
const restaurantDoc = await db.collection("restaurants").doc(restaurantId).get(); // unnecessary db-read!
const {cook} = restaurantDoc.data();
await writeToSomeWallOfFame(cook);
res.send();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment