I am using Cognito user pool to authenticate users in my system. A successful authentication gives an ID Token (JWT), Access Token (JWT) and a Refresh
Using NodeJS aws-sdk and a bit of Promise you can await authentication using Refresh Token with initiateAuth as follows:
const {CognitoIdentityServiceProvider} = require('aws-sdk');
const initiateAuth = (ClientId, REFRESH_TOKEN, DEVICE_KEY) =>
new Promise((resolve, reject) => {
const CISP = new CognitoIdentityServiceProvider();
CISP.initiateAuth(
{
ClientId, // Cognito App Client Id
AuthFlow: 'REFRESH_TOKEN_AUTH',
AuthParameters: {
REFRESH_TOKEN,
DEVICE_KEY
}
},
(err, data) => {
if (err) {
return reject(err);
}
resolve(data);
}
);
});
// ------ Usage ------ //
(async () => {
const tokens = await initiateAuth('mY4pps3cR3T', '<R3FR3SHT0K3N>');
console.log('Tokens', tokens);
const {AuthenticationResult: {AccessToken, IdToken, ExpiresIn, TokenType}} = tokens;
})()
Keep in mind that if Device tracking is enabled you should pass a device key otherwise you can receive Invalid refresh token error.