title: AWS Cognito - Invalid Refresh Token
body: <p>I am using the <a href="https://aws.amazon.com/cognito/" rel="noreferrer">Amazon Cognito</a> service with the <a href="https://github.com/aws/amazon-cognito-identity-js" rel="noreferrer">amazon-cognito-identity-js</a> library, and am having an issue refreshing a user's tokens, namely the id token.</p>
  <p>When trying to refresh the users tokens by making an unauthenticated <a href="http://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html" rel="noreferrer">initiateAuth</a> request, I receive a 400 http status in response, along with an &quot;Invalid Refresh Token&quot; error message.</p>
  <blockquote>
  <p>POST <a href="https://cognito-idp.us-east-1.amazonaws.com/" rel="noreferrer">https://cognito-idp.us-east-1.amazonaws.com/</a> 400 (Bad Request)</p>
  <p>Uncaught Error: Invalid Refresh Token.</p>
  </blockquote>
  <p>Why does it believe that I am passing in invalid refresh token?</p>
  <pre><code>// the refresh token
  var reToken;
  
  // pool config
  var poolData = {
      UserPoolId : 'us-east-1_XXXXXXXXX',
      ClientId : 'XXXXXXXXXXXXXXXXXXXXXXXXXX'
  };
  
  // connect to user pool and 
  // find the current user
  var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
  var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
  var cognitoUser = userPool.getCurrentUser();
  
  // if we found a user
  if (cognitoUser != null)
  {
      // get active user session
      cognitoUser.getSession(function(err, session)
      {
          // catch errors
          if (err) {
              alert(err);
              return;
          }
  
          // get the refresh token
          reToken = session.refreshToken.token;
      });
  }
  
  // get current epoch time
  var curDate = new Date();
  var currentEpoch = Math.round(curDate.getTime() / 1000);
  
  // get the epoch when the token
  // was last issued
  var issuedEpoch = store.get('issued');
  
  // set the refresh parameters
  var refreshParams = {
      ClientId: 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
      AuthFlow: 'REFRESH_TOKEN_AUTH',
      AuthParameters: { 'REFRESH_TOKEN': reToken }
  };
  
  // note: 30 minutes = 1800 seconds
  // if a token was last issued over 30 minutes ago
  if ( (currentEpoch - issuedEpoch) &gt;= 1800 )
  {
      // refresh the users token with a new token
      userPool.client.makeUnauthenticatedRequest('initiateAuth', refreshParams, (err, newToken) =&gt; {
          // catch errors
          if (err) {
              alert(err);
              return;
          }
  
          // do stuff with the returned token
          console.log(newToken)
      })
  
  }
  </code></pre>
  <p>As an aside, i've tried using refreshSession() but it tells me that getToken() is not a function of refreshSession().</p>
  <pre><code>cognitoUser.refreshSession(reToken, (err, authResult) =&gt; {
      if (err) throw err;
      console.log(authResult)
  });
  </code></pre>