isAuthenticated method

Future<bool> isAuthenticated()

Implementation

Future<bool> isAuthenticated() async {
  dart_blocks.User currentUser = await getCurrentUser();
  if (currentUser.id != "") {
    try {
      var accessToken = await getAccessToken();
      if (accessToken != "" &&
          JwtDecoder.getRemainingTime(accessToken).inMinutes > 2) {
        try {
          if (_jwtPublicKey == "") {
            throw Exception("empty jwt public key");
          }
          JWT.verify(accessToken, RSAPublicKey(_jwtPublicKey ?? ""));
          return true;
        } catch (e) {
          if (debug == true)
            print("could not verify token with err" + e.toString());
        }
      }
      // token is expired - refresh
      dart_blocks.UserRequest req = dart_blocks.UserRequest();
      req.cloudToken = await _authorize.getAccessToken();
      // set metadata for token
      dart_blocks.Token _token = dart_blocks.Token();
      _token.deviceInfo = await _getDeviceInfo();
      Placemark? _placemark = this._placemark ?? await _determinePlacemark();
      if (_placemark != null) {
        dart_blocks.Location _location = dart_blocks.Location();
        _location.country = _placemark.country ?? "";
        _location.countryCode = _placemark.isoCountryCode ?? "";
        _location.city = _placemark.locality ?? "";
        _token.loggedInFrom = _location;
      }
      _token.refreshToken = await _getRefreshToken();
      req.token = _token;
      dart_blocks.UserResponse refreshResp =
          await _grpcUserClient.refreshToken(req);
      _setAccessToken(refreshResp.token.accessToken);
      _setRefreshToken(refreshResp.token.refreshToken);
      return true;
    } catch (e) {
      if (debug == true)
        print("could not refresh token with err: " + e.toString());
      return false;
    }
  }
  return false;
}