amazon-cognito-identity-js 刷新令牌过期处理

amazon-cognito-identity-js refresh token expiration handling

通过获取会话检索 ID 令牌时,如果访问令牌已过期,Cognito Identity js 会自动使用其刷新令牌检索新的访问令牌。但是,如果刷新令牌也已过期,我想实施正确的处理,但很难测试,因为刷新令牌的最短到期时间为 1 天。

很高兴知道是否:

代码:

getIdToken(callback: Callback): void {
if (callback == null) {
  throw("callback is null");
}
if (this.getCurrentUser() != null) {
  this.getCurrentUser().getSession(function (err, session) {
    if (err) {
      console.log("error: " + err);
      callback.callbackWithParam(null);
    } else {
      if (session.isValid()) {
        console.log("returning id token");
        callback.callbackWithParam(session.getIdToken().getJwtToken());
      } else {
        console.log("got the id token, but the session isn't valid");
      }
    }
  });
  } 
  else
    callback.callbackWithParam(null);
}

我的猜测是 got the id token, but the session isn't valid 将被调用,因为当刷新令牌有效时它会自动刷新访问令牌并且会话再次有效。

登录 Kibana 时收到以下消息:

com.amazonaws.services.cognitoidp.model.NotAuthorizedException: Refresh Token has expired (Service: AWSCognitoIdentityProvider; Status Code: 400; Error Code: NotAuthorizedException; Request ID: ...)

在这种情况下,错误分支将被调用

if (err) {
  console.log("error: " + err);
  callback.callbackWithParam(null);
}

所以刷新令牌过期的处理需要在那里完成。但是,除了 session.isValid()

之外,我决定在每种情况下都将用户重定向到登录页面

希望这对外面的人有帮助:)