I'm trying to create a wrapper called loginRequired
for my Express route, but I'm struggling to define the right types.
This is what my loginRequired
wrapper looks like:
export const loginRequired = (fn) => (req, res, next) => {
// Check if unauthorized
if (!req.session.currentUser) {
return res.status(401).send({
status: 'error',
message: 'Login required'
});
}
// If authorized, execute normally
return fn(req, res, next);
}
Now I need to wrap my route handler:
app.get('/protected', loginRequired(req, res, next) => {
return res.next({ secret: 'foo' });
});