Trying browser pub/sub in react.js (Not working) #421
-
It does not work when using "auth.CredentialsProvider" from aws-iot-device-sdk-v2 (auth) because it says Property 'CredentialsProvider' may not exist, however 'AwsCredentialsProvider' is declared here, but using it does not work. class AWSCognitoCredentialsProvider extends auth.CredentialsProvider{
private options: AWSCognitoCredentialOptions;
private cachedCredentials? : CognitoIdentityCredentials;
constructor(options: AWSCognitoCredentialOptions, expire_interval_in_ms? : number)
{
super();
this.options = options;
setInterval(async ()=>{
await this.refreshCredentials();
},expire_interval_in_ms?? 3600*1000);
}
getCredentials() : auth.AWSCredentials {
return {
aws_access_id: this.cachedCredentials?.accessKeyId ?? "",
aws_secret_key: this.cachedCredentials?.secretAccessKey ?? "",
aws_sts_token: this.cachedCredentials?.sessionToken,
aws_region: this.options.Region
}
}
async refreshCredentials() {
log('Fetching Cognito credentials');
this.cachedCredentials = await fromCognitoIdentityPool({
// Required. The unique identifier for the identity pool from which an identity should be
// retrieved or generated.
identityPoolId: this.options.IdentityPoolId,
clientConfig: { region: this.options.Region },
})();
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You're using node-only functionality (the SDK Cognito provider) in an environment that's going to be using the browser implementation. The react sample (https://github.com/aws/aws-iot-device-sdk-js-v2/blob/main/samples/browser/react_sample/src/PubSub5.tsx) shows sourcing credentials from Cognito in browser mode by importing the v3 JS SDK's provider implementation and adapting to the IoT SDK's interface. Edit: Ideally, please don't edit/rewrite an initial question with a different approach. From a searchability and issue-resolution standpoint, this reply, which was correct yesterday, is now completely irrelevant. |
Beta Was this translation helpful? Give feedback.
-
Hi, I am having difficulties using react sample code |
Beta Was this translation helpful? Give feedback.
@github-actions proposed-answer
You need to make sure that your tsconfig.json links to the browser path rather than the default node.js. ie. it need to look like this: