SHARE
TWEET

Untitled

a guest Aug 18th, 2019 143 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Amplify Params - DO NOT EDIT
  2. You can access the following resource attributes as environment variables from your Lambda function
  3. const environment = process.env.ENV
  4. const region = process.env.REGION
  5. const authLambdauser<some_number>UserPoolId = process.env.AUTH_LAMBDAUSERSOMENUMBER_USERPOOLID
  6.  
  7. Amplify Params - DO NOT EDIT */
  8.  
  9. const express = require('express');
  10. const bodyParser = require('body-parser');
  11. const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');
  12. const AWS = require('aws-sdk');
  13.  
  14. const app = express();
  15. app.use(bodyParser.json());
  16. app.use(awsServerlessExpressMiddleware.eventContext());
  17.  
  18. app.use(function(req, res, next) {
  19.   res.header('Access-Control-Allow-Origin', '*');
  20.   res.header(
  21.     'Access-Control-Allow-Headers',
  22.     'Origin, X-Requested-With, Content-Type, Accept'
  23.   );
  24.   next();
  25. });
  26.  
  27. app.get('/items', async function(req, res) {
  28.   try {
  29.     const IDP_REGEX = /.*\/.*,(.*)\/(.*):CognitoSignIn:(.*)/;
  30.     const authProvider =
  31.       req.apiGateway.event.requestContext.identity
  32.         .cognitoAuthenticationProvider;
  33.     const [, , , userId] = authProvider.match(IDP_REGEX);
  34.  
  35.     const cognito = new AWS.CognitoIdentityServiceProvider();
  36.     const listUsersResponse = await cognito
  37.       .listUsers({
  38.         UserPoolId: process.env.AUTH_LAMBDAUSERSOMENUMBER_USERPOOLID,
  39.         Filter: `sub = "${userId}"`,
  40.         Limit: 1,
  41.       })
  42.       .promise();
  43.     const user = listUsersResponse.Users[0];
  44.     res.json({ user, message: 'get call succeed!', url: req.url });
  45.   } catch (error) {
  46.     console.log(error);
  47.     res.json({ error, message: 'get call failed' });
  48.   }
  49. });
  50.  
  51. app.listen(3000, function() {
  52.   console.log('App started');
  53. });
  54.  
  55. module.exports = app;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top