У меня есть простой маршрут отправки маршрута в express, для которого я создал промежуточное ПО для аутентификации.
app.post("/add-recipe", FireBaseAuth, (req, res) => {
if (req.body.body.trim() === "") {
return res.status(400).json({ body: "Recipe cannot be empty" });
}
const newRecipe = {
body: req.body.body,
userHandle: req.user.handle,
createdAt: new Date().toISOString(),
};
db.collection("recipes")
.add(newRecipe)
.then((doc) => {
res.json({ message: `document ${doc.id} created sucessfully` });
})
.catch((err) => {
res.status(500).json({ error: "something went wrong" });
console.error(err);
});
});
Когда я добавил свое промежуточное ПО для аутентификации (ниже), я получаю следующую ошибку: 1005 *
Вот промежуточное ПО
const FireBaseAuth = (req, res, next) => {
let idToken;
if (
req.headers.authorization &&
req.headers.authorization.startsWith("Bearer ")
) {
idToken = req.headers.authorization.split("Bearer ")[1];
} else {
console.error("No token found");
return res.status(403).json({ error: "Unauthorized" });
}
admin //verify token was issued by the food app and not someone else
.auth()
.verifyIdToken(idToken)
.then((decodedToken) => {
req.user = decodedToken;
console.log(decodedToken);
return db
.collection("users")
.where("userId", "==", req.user.uid)
.limit(1)
.get();
})
.then((data) => {
req.user.handle = data.docs[0].data().handle;
return next(); //allows request to proceed.
})
.catch((err) => {
console.error("Error while verifying token", err);
return res
.status(403)
.json({ Error: "You are not authorized to make this request" });
});
};
Я не могу понять, как t0, почему это вызывает эту ошибку.