im making a middleware from a node api, to check if the user is logged in, and im sending a jwt token
this is the class and the function
export class Auth {
async isAuthenticatedClient(req: Request, res: Response) {
const { token } = req.cookies
if (!token) {
res.status(401).send({
message: "Faça login para acessar esse conteúdo"
})
}
const decoded = jwt.verify(token, "sgjlskdfghajhdfghsd");
req.client = await prisma.client.findUnique({ decoded.id });
}
}
its returning two errors:
Property 'client' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
Argument of type '{ decoded: string | jwt.JwtPayload; "": any; }' is not assignable to parameter of type '{ rejectOnNotFound?: RejectOnNotFound | undefined; select?: ClientSelect | null | undefined; include?: ClientInclude | null | undefined; where: ClientWhereUniqueInput; }'.
Object literal may only specify known properties, and 'decoded' does not exist in type '{ rejectOnNotFound?: RejectOnNotFound | undefined; select?: ClientSelect | null | undefined; include?: ClientInclude | null | undefined; where: ClientWhereUniqueInput; }'.
im using a js api to base myself, and the type of client should be any, and decoded should be jwt.Jwt & jwt.JwtPayload & void
EDIT: i created two interfaces and its not getting any error in the class, but in the routes the following line:
router.route('/teste').get(authController.teste, authMiddleware.isAuthenticatedClient) // this route only gets a simples request just to test the middleware
these are the two interfaces that i created:
export interface GetClientAuthInfoRequest extends Request {
client: Client | null// or any other type
}
interface JwtPayload {
id: string
}
export type Client = {
name: string
email: string
password: string
}
the error is the following:
No overload matches this call.
The last overload gave the following error.
Argument of type '(req: GetClientAuthInfoRequest, res: Response<any, Record<string, any>>) => Promise<void>' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
Type '(req: GetClientAuthInfoRequest, res: Response<any, Record<string, any>>) => Promise<void>' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
Types of parameters 'req' and 'req' are incompatible.
Property 'client' is missing in type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' but required in type 'GetClientAuthInfoRequest'.ts(2769)
Express.type.ts(4, 5): 'client' is declared here.
index.d.ts(222, 5): The last overload is declared here.