Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Config, CognitoIdentityCredentials, DynamoDB } from 'aws-sdk';
- import { CognitoUserPool, CognitoUserAttribute,
- CognitoUser, AuthenticationDetails } from 'amazon-cognito-identity-js';
- import awsConfig from '../config';
- Config.region = awsConfig.region;
- const userPool = new CognitoUserPool({
- UserPoolId: awsConfig.UserPoolId,
- ClientId: awsConfig.UserPoolClientId,
- });
- export function signinAndGetdata(username, password) {
- const authenticationData = {
- Username: username,
- Password: password,
- };
- const authenticationDetails = new AuthenticationDetails(authenticationData);
- const userData = {
- Username: username,
- Pool: userPool,
- };
- const cognitoUser = new CognitoUser(userData);
- return new Promise((resolve, reject) => {
- cognitoUser.authenticateUser(authenticationDetails, {
- onSuccess: function (result) {
- AWS.config.credentials = new CognitoIdentityCredentials({
- IdentityPoolId: awsConfig.IdentityPoolId, // your identity pool id here
- Logins: {
- [`cognito-idp.${awsConfig.region}.amazonaws.com/${awsConfig.UserPoolId}`]: result.getIdToken().getJwtToken(),
- },
- });
- AWS.config.region = awsConfig.region;
- console.log(cognitoUser);
- // Instantiate aws sdk service objects now that the credentials have been updated.
- // example: var s3 = new AWS.S3();
- AWS.config.credentials.refresh((error) => {
- AWS.config.credentials.get(function (err) {
- if (err) {
- console.log(err)
- reject(err);
- } else {
- console.log(AWS.config.credentials.identityId);
- const docClient = new AWS.DynamoDB.DocumentClient();
- const params = {
- TableName: 'WavycloudDevelopment-dev-frontend-yr-CallLoggerDynamoDbTable-1DWRDT1NYFLGJ',
- IndexName: 'user_id_AND_timestamp',
- KeyConditionExpression: 'user_id = :user_id and #TIMESTAMP > :date',
- ExpressionAttributeValues: {
- ':user_id': AWS.config.credentials.identityId,
- ':date': '2016-11-19T23:17:10.481Z'
- },
- ExpressionAttributeNames: {
- '#TIMESTAMP': 'timestamp',
- }
- };
- docClient.query(params, function(err, data) {
- if (err) {
- console.log(err);
- } else {
- console.log(data);
- resolve(data);
- }
- });
- }
- });
- });
- },
- onFailure: function(err) {
- reject(err);
- },
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement