i wrote a code for the closed endpoints to privilege the routes, up to middleware everything is working fine but when i try to use that endpoint the error is coming.
Error Message:
No overload matches this call. The last overload gave the following error. Argument of type '(req: User, res: express.Response, next: express.NextFunction) => express.Response | Promise<void | express.Response>' is not assignable to parameter of type 'PathParams'.
This is my code
//controller.ts file
import { ProductModel } from "./model";
import * as express from "express";
import { endPoint } from "../../helpers/endpoint";
import { isAdmin } from "../../middlewares/middlewares";
const router = express.Router();
router
.use(endPoint, isAdmin)
.post("/admin/addProduct", (req: any, res: express.Response) => {
const obj = new ProductModel(req.user);
obj
._create_product(req.body)
.then(() => {
return res
.status(201)
.json({ message: "New Product created successfully" });
})
.catch((err) => {
return res.status(404).json({ error: "Failed to create product", err });
});
});
export default router;
//endpoint.ts file
import { admin, db } from "../config/admin";
import * as express from "express";
interface User extends express.Request{
user:{
email:string,
uid:string
}
}
export const endPoint = (
req:User,
res: express.Response,
next: express.NextFunction
) => {
if (!req.headers && !req.headers["authorization"]) {
return res.status(404).json({ error: "UnAuthorised" });
} else {
const bearer: any = req.headers["authorization"];
const token: any = bearer.split("Bearer ")[1];
return admin
.auth()
.verifyIdToken(token)
.then((decoded) => {
console.log("decoded",decoded);
req.user.uid=decoded.uid
console.log("req.user",req.user.uid)
return db
.collection("USERS")
.where("uid", "==", req.user)
.limit(1)
.get();
})
.then((userData) => {
console.log(userData)
req.user.email= userData.docs[0].data().email
return next();
})
.catch((error) => {
console.error(error);
if (error.code === "auth/id-token-expired") {
return res.status(401).json({
message: `Token has expired please try again!!! with new Token`,
});
}
return res.status(500).json({ message: `invalid token` });
});
}
};