Reputation: 183
I am trying to replicate a middleware you might have in express on an azure function.
For example:
router.get('/protectedEndpoint', secured(), function (req, res) {
where the secured() function is a middleware that will send next() if valid.
the problem with azure is it is done in the style
module.exports = function (context) {
and i am unsure of how to run a middleware with next() in this
here is a dumb example of what that function may look like:
module.exports = function () {
return function secured (req, res, next) {
if (req.user) { return next(); }
req.session.returnTo = req.originalUrl;
res.redirect('/login');
};
};
Upvotes: 3
Views: 2907
Reputation: 95
Yes well that example on the page is cryptic at best, and as it turns out, the package does not have full Typescript support yet either.
If anyone reading this is looking for a Typescript solution, you will have to embrace nested try catch statements to first verify the token (if the use case is Authentication), then proceed with the service to call any protected resources.
Upvotes: 1
Reputation: 783
With azure function you can use azure-middleware engine to add middleware just like you do with Express. By this method you can do the same thing as you were doing with Express. The link for this Engine is as follow azure-middlware
var MiddlewareHandler = require('azure-middleware')
module.exports = new MiddlewareHandler()
.use((ctx) => {
secured();//Your middleware function
ctx.next();
})
.use(async (ctx) => {
//Your controller or your main function script
ctx.log.info('Im called third');
ctx.done(null, { status: 200 });
})
.catch((error, ctx, msg) => {
ctx.log.info(error); // ERROR!
ctx.next();
})
.listen();
Upvotes: 1