Reputation: 14814
I want to use an async
function in my middleware that extracts the user in my AWS Lambda function.
Here is the code:
const bodyParser = require('body-parser');
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');
const AWS = require('aws-sdk');
const asyncHandler = fn => (req, res, next) =>
Promise.resolve(fn(req, res, next)).catch(next);
const pipe = (...fns) => x => fns.reduce((y, f) => f(y), x);
const tapRoute = f => route => {
route.use(f);
return route;
};
const configureCors = route => {
route.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});
};
const injectUser = route => {
route.use(async function(req, res, next) {
try {
const IDP_REGEX = /.*\/.*,(.*)\/(.*):CognitoSignIn:(.*)/;
const authProvider =
req.apiGateway.event.requestContext.identity
.cognitoAuthenticationProvider;
const [, , , userId] = authProvider.match(IDP_REGEX);
console.log(userId);
console.log('Got user id');
const cognito = new AWS.CognitoIdentityServiceProvider();
const listUsersResponse = await cognito
.listUsers({
UserPoolId: process.env.AUTH_LAMBDAUSERB2E6BC69_USERPOOLID,
Filter: `sub = "${userId}"`,
Limit: 1,
})
.promise();
console.log('ListedUsers');
const user = listUsersResponse.Users[0];
req.user = user;
next();
} catch (error) {
console.log(error);
next(error);
} finally {
return route;
}
});
};
const applyMiddleware = (route, ...middleware) =>
pipe(
tapRoute(bodyParser.json()),
tapRoute(awsServerlessExpressMiddleware.eventContext()),
configureCors,
injectUser,
...middleware
)(route);
module.exports = {
applyMiddleware,
};
The code throws an error in this line:
route.use(async function(req, res, next) {
And here is the error:
module initialization error: TypeError
at injectUser (/var/task/middleware.js:25:9)
at fns.reduce (/var/task/middleware.js:8:52)
at Array.reduce (<anonymous>)
at x (/var/task/middleware.js:8:35)
at applyMiddleware (/var/task/middleware.js:63:4)
at Object.<anonymous> (/var/task/app.js:13:1)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
Generally, I later use this middleware like this:
const express = require('express');
const applyMiddleware = require('./middleware').applyMiddleware;
const app = express();
applyMiddleware(app);
app.get( // ... rest of the code
But sometimes I also inject middleware on a per route basis.
What is going on? Why is this TypeError being thrown? Is there a way I can use the async middleware that injects the user in my pipe, so that I can reduce my boilerplate? If I comment out injectUser
the middleware works...
Edit:
Using app.use(async function // ...
works, but I'd like to have the async function in my pipe
aka. in applyMiddleware
.
Upvotes: 0
Views: 862
Reputation: 146630
The problem is your configureCors
function. You don't return anything there, so the chain gets broken and injectUser
gets a undefined
route
variable. It should be updated like below
const configureCors = route => {
route.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});
return route;
};
Upvotes: 2