refreshSession method

Future<CognitoUserSession?> refreshSession(
  1. CognitoRefreshToken refreshToken
)

This uses the refreshToken to retrieve a new session

Implementation

Future<CognitoUserSession?> refreshSession(
    CognitoRefreshToken refreshToken) async {
  final authParameters = {
    'REFRESH_TOKEN': refreshToken.getToken(),
  };

  if (await storage.getItem(pool.lastUserKey) != null) {
    username = await storage.getItem(pool.lastUserKey);
    final deviceKeyKey = '$keyPrefix.deviceKey';
    _deviceKey = await storage.getItem(deviceKeyKey);
    authParameters['DEVICE_KEY'] = _deviceKey;
  }

  if (_clientSecret != null) {
    authParameters['SECRET_HASH'] = _clientSecret;
  }

  final paramsReq = {
    'ClientId': pool.getClientId(),
    'AuthFlow': 'REFRESH_TOKEN_AUTH',
    'AuthParameters': authParameters,
  };
  if (getUserContextData() != null) {
    paramsReq['UserContextData'] = getUserContextData();
  }

  dynamic authResult;
  try {
    authResult = await client!.request('InitiateAuth',
        await _analyticsMetadataParamsDecorator.call(paramsReq));
  } on CognitoClientException catch (e) {
    if (e.code == 'NotAuthorizedException') {
      await clearCachedTokens();
    }
    rethrow;
  }

  if (authResult != null) {
    final authenticationResult = authResult['AuthenticationResult'];
    if (authenticationResult['RefreshToken'] == null) {
      authenticationResult['RefreshToken'] = refreshToken.getToken();
    }
    _signInUserSession = getCognitoUserSession(authenticationResult);
    await cacheTokens();
    return _signInUserSession;
  }
  return null;
}