I get this error:
Server.ts
import * as express from 'express';
import {Application} from "express";
import * as fs from 'fs';
import * as https from 'https';
import {retrieveUserId} from './user-middleware-extraction';
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const app: Application = express();
app.use(cookieParser());
app.use(retrieveUserId);
app.use(bodyParser.json());
` User-middleware-extraction.ts
import { NextFunction,Request,Response } from "express";
import { decodeToken } from "./security.utils";
export function retrieveUserId(res:Response,req:Request,next:NextFunction) {
const jwt = req.cookies['SESSIONID'] // retrieving the jwtoken
if(jwt) {
handleSession(jwt, req)
.then(()=>
next())
.catch(err=>
{
console.log(err);
next()
})
}
}
async function handleSession(jwt, req) {
try {
req['userId'] = payload.sub//request contain the id
var payload = await decodeToken(jwt)//payload of the jwt
}
catch(err) {
console.log("error is", err)
}
}
While assigning the middleware to server.ts this error is popping up. No idea what is causing this to happen.
Is the issue with the middleware functionality?
CodePudding user response:
Sorry, my bad. It seems like you just have to exchange req and res function parameters in order to get matching types.
Order of path params should be: (req, res, next)
. You switched up request and response params.