Skip to content

Instantly share code, notes, and snippets.

@jwoyo
Created December 10, 2019 08:23
Show Gist options
  • Save jwoyo/dc694cd40fb4144fe07630219bf3b5a3 to your computer and use it in GitHub Desktop.
Save jwoyo/dc694cd40fb4144fe07630219bf3b5a3 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 restaurant = restaurantDoc.data();
if (restaurant.owner !== req.user.uid) {
res.status(403).send("Unauthorized");
return;
}
req.restaurant = restaurant; // extending request object
next();
};
app.put("/:restaurantId/stars", [onlyLoggedInUsers, restaurantOwnerOnlyMiddleware], (req, res) => {
// const restaurantDoc = await db.collection("restaurants").doc(restaurantId).get(); // avoided db-read!
const {cook} = req.restaurant;
await writeToSomeWallOfFame(cook);
res.send();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment