assignNewTokens static method

Future<bool> assignNewTokens()

Implementation

static Future<bool> assignNewTokens() async {
  if (!isTokenExpired()) return true;
  if (ConfigService.checkValueExist("refreshTokenExp")) {
    //checks if refresh token is still valid
    if (DateTime.fromMillisecondsSinceEpoch(
            ConfigService.refreshTokenExpDate * 1000)
        .isAfter(DateTime.now())) {
      //overrides access token with refresh token
      ConfigService.token = ConfigService.refreshToken;
      OperationResult response = await getRefreshTokens();
      if (response.success) {
        ConfigService.token = response.data['token'];
        ConfigService.refreshToken = response.data['refreshToken'];
        ConfigService.accessTokenExpDate = response.data['tokenExp'];
        ConfigService.refreshTokenExpDate = response.data['refreshTokenExp'];
        return true;
      }
    }
  }
  return false;
}