rp
03/01/2022, 5:39 PMts
function myCustomMiddleware(verifySessionOptions?: Session.VerifySessionOptions) {
return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
try {
let session = await Session.getSession(req, verifySessionOptions);
if (session === undefined) {
if (verifySessionOptions?.sessionRequired === false) {
return next();
} else {
throw Error("Should never come here");
}
}
let userId = session.getUserId();
// TODO: fetch info from mongo and attach to req.user = ....
next();
} catch (err) {
next(err);
}
}
}
app.get("/some-api", myCustomMiddleware(), (req, res) => {
// TODO:...
})
note that the above requires that you have added the supertokens errorHandler to the your express app (should be done if you follow the backend quick setup correctly).