assignNewTokens static method

Future<bool> assignNewTokens()

Implementation

static Future<bool> assignNewTokens() async {
  try {
    if (!isTokenExpired()) return true;
    if (ConfigService.checkValueExist("refreshTokenExpDate")) {
      DateTime dateTimeFromMilliseconds = DateTime.fromMillisecondsSinceEpoch(
          ConfigService.refreshTokenExpDate * 1000);
      DateTime now = DateTime.now();
      //checks if refresh token is still valid
      if (dateTimeFromMilliseconds.isBefore(now)) {
        debugPrint("-----> overrides access token with refresh token");
        //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;
        } else {
          return false;
        }
      }
    }
    return true;
  } catch (ex) {
    return false;
  }
}