Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Amplify Params - DO NOT EDIT
- You can access the following resource attributes as environment variables from your Lambda function
- const environment = process.env.ENV
- const region = process.env.REGION
- const authLambdauser<some_number>UserPoolId = process.env.AUTH_LAMBDAUSERSOMENUMBER_USERPOOLID
- Amplify Params - DO NOT EDIT */
- const express = require('express');
- const bodyParser = require('body-parser');
- const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');
- const AWS = require('aws-sdk');
- const app = express();
- app.use(bodyParser.json());
- app.use(awsServerlessExpressMiddleware.eventContext());
- app.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();
- });
- app.get('/items', async function(req, res) {
- try {
- const IDP_REGEX = /.*\/.*,(.*)\/(.*):CognitoSignIn:(.*)/;
- const authProvider =
- req.apiGateway.event.requestContext.identity
- .cognitoAuthenticationProvider;
- const [, , , userId] = authProvider.match(IDP_REGEX);
- const cognito = new AWS.CognitoIdentityServiceProvider();
- const listUsersResponse = await cognito
- .listUsers({
- UserPoolId: process.env.AUTH_LAMBDAUSERSOMENUMBER_USERPOOLID,
- Filter: `sub = "${userId}"`,
- Limit: 1,
- })
- .promise();
- const user = listUsersResponse.Users[0];
- res.json({ user, message: 'get call succeed!', url: req.url });
- } catch (error) {
- console.log(error);
- res.json({ error, message: 'get call failed' });
- }
- });
- app.listen(3000, function() {
- console.log('App started');
- });
- module.exports = app;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement