I’m trying to send a message to SQS with the Javascript v3 SDK, using Cognito already logged in user session credentials.
Cognito login
This works fine and I can use the session IdToken
and AccessToken
with no problem to authenticate with Api Agteway, for example.
window.authToken = new Promise(function fetchCurrentAuthToken(resolve, reject) {
cognitoUser.getSession(function sessionCallback(err, session) {
if (err) {
reject(err);
} else if (!session.isValid()) {
resolve(null);
} else {
resolve(session.getIdToken().getJwtToken());
console.log(session.getIdToken());
window.token = session.getIdToken().getJwtToken();
window.logged_user = decodeJWT(token);
let credentials = {
accessToken: session.getAccessToken().getJwtToken(),
idToken: token,
};
sendMessageToSQS(token, 'Oh yes baby', credentials)
};
});
});
SQS
Then I try to send a message with the following function, but I get the error that credentials object is not valid.
function sendMessageToSQS(idToken, messageBody, credentials) {
let sqsClient = new SQSClient({
'region': 'ap-southeast-2',
'credentials': credentials
});
const params = {
'QueueUrl': "myQueueUrl",
'MessageBody': messageBody,
};
const headers = {
'Content-Type': 'application/json',
'Authorization': idToken,
};
const command = new SendMessageCommand(params);
const response = sqsClient.send(command, { headers });
console.log("Message sent:", response);
};
I found diverse and confusing information about this. Wht should the “credentials” for the SQS request should look like?