Advertisement
Guest User

Untitled

a guest
Dec 29th, 2016
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Config, CognitoIdentityCredentials, DynamoDB } from 'aws-sdk';
  2. import { CognitoUserPool, CognitoUserAttribute,
  3.   CognitoUser, AuthenticationDetails } from 'amazon-cognito-identity-js';
  4. import awsConfig from '../config';
  5.  
  6. Config.region = awsConfig.region;
  7. const userPool = new CognitoUserPool({
  8.   UserPoolId: awsConfig.UserPoolId,
  9.   ClientId: awsConfig.UserPoolClientId,
  10. });
  11.  
  12. export function signinAndGetdata(username, password) {
  13.   const authenticationData = {
  14.     Username: username,
  15.     Password: password,
  16.   };
  17.   const authenticationDetails = new AuthenticationDetails(authenticationData);
  18.   const userData = {
  19.     Username: username,
  20.     Pool: userPool,
  21.   };
  22.   const cognitoUser = new CognitoUser(userData);
  23.   return new Promise((resolve, reject) => {
  24.     cognitoUser.authenticateUser(authenticationDetails, {
  25.       onSuccess: function (result) {
  26.         AWS.config.credentials = new CognitoIdentityCredentials({
  27.           IdentityPoolId: awsConfig.IdentityPoolId, // your identity pool id here
  28.           Logins: {
  29.             [`cognito-idp.${awsConfig.region}.amazonaws.com/${awsConfig.UserPoolId}`]: result.getIdToken().getJwtToken(),
  30.           },
  31.         });
  32.         AWS.config.region = awsConfig.region;
  33.         console.log(cognitoUser);
  34.         // Instantiate aws sdk service objects now that the credentials have been updated.
  35.         // example: var s3 = new AWS.S3();
  36.         AWS.config.credentials.refresh((error) => {
  37.         AWS.config.credentials.get(function (err) {
  38.             if (err) {
  39.                 console.log(err)
  40.                reject(err);
  41.             } else {  
  42.                 console.log(AWS.config.credentials.identityId);                
  43.                 const docClient = new AWS.DynamoDB.DocumentClient();
  44.                 const params = {
  45.                   TableName: 'WavycloudDevelopment-dev-frontend-yr-CallLoggerDynamoDbTable-1DWRDT1NYFLGJ',
  46.                   IndexName: 'user_id_AND_timestamp',
  47.                   KeyConditionExpression: 'user_id = :user_id and #TIMESTAMP > :date',
  48.                   ExpressionAttributeValues: {
  49.                     ':user_id': AWS.config.credentials.identityId,
  50.                     ':date': '2016-11-19T23:17:10.481Z'
  51.                   },
  52.                   ExpressionAttributeNames: {
  53.                     '#TIMESTAMP': 'timestamp',
  54.                   }
  55.                 };
  56.  
  57.                 docClient.query(params, function(err, data) {
  58.                     if (err) {
  59.                         console.log(err);
  60.                     } else {
  61.                         console.log(data);
  62.                         resolve(data);
  63.                     }
  64.                 });
  65.             }
  66.         });
  67.         });
  68.        
  69.        
  70.       },
  71.  
  72.       onFailure: function(err) {
  73.         reject(err);
  74.       },
  75.  
  76.     });
  77.   });
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement