Created
December 10, 2019 08:23
-
-
Save jwoyo/dc694cd40fb4144fe07630219bf3b5a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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