amazon-archives / amazon-cognito-identity-js

Amazon Cognito Identity SDK for JavaScript
Other
985 stars 451 forks source link

Integration with DynamoDB #249

Closed howardya closed 7 years ago

howardya commented 7 years ago

Hi, can I ask, how can I integrate this package with the DynamoDB? I only want authenticated users to have access to my dynamo database. Greatly appreciate any advice or help with this.

import {AuthenticationDetails, CognitoUser, CognitoUserPool} from 'amazon-cognito-identity-js';

const userPool = new CognitoUserPool({
  UserPoolId: 'myuserpoolid',
  ClientId: 'myclientid',
});

const authenticationDetails = new AuthenticationDetails({
  Username: 'username',
  Password: 'password'
});

const userData = {
  Username: 'username',
  Pool: userPool
};

const cognitoUser = new CognitoUser(userData);

cognitoUser.authenticateUser(authenticationDetails, {
  onSuccess(result) {
    // success
    const dynamodb = new DynamoDB();
    // **how to pass the credentials to DynamoDB**
  },
  onFailure(err) {
    // error
  },
});
itrestian commented 7 years ago

You would have to integrate with Cognito Identity to obtain AWS credentials. Basically use case 17. Follow the docs to do that and after obtaining credentials, you can use the AWS SDK to make calls to Dynamo:

http://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-integrating-user-pools-with-identity-pools.html

howardya commented 7 years ago

I do not have AWS as a global variable and do not wish to. I only want to import the relevant services. So I am not sure how to apply Use Case 17 to my case.

What I am looking for is a way to

const dynamodb = new DynamoDB({
  credentials: ???
});
howardya commented 7 years ago

After referring to this I have modified it to be the following

let Logins = {};
Logins[`cognito-idp.${appConfig.region}.amazonaws.com/${appConfig.UserPoolId}`] = 'xxx';
const myCredentials = new CognitoIdentityCredentials({
  IdentityPoolId: appConfig.IdentityPoolId,
  region: appConfig.region,
  Logins: Logins
});

const dynamodb = new DynamoDB({
  IdentityPoolId: appConfig.IdentityPoolId,
  region: appConfig.region,
  credentials: myCredentials
});

I still have Error: Missing credentials in config

itrestian commented 7 years ago

This SDK won't give you the ability to access DynamoDB. You should use the main AWS JavaScript SDK for that. You can build a slimmed down version of the AWS SDK that only accesses DynamoDB following instructions on their page.

itrestian commented 7 years ago

You would also need to make a call to refresh or get to actually retrieve the credentials.