isAuthenticated method

Future<bool> isAuthenticated()

Implementation

Future<bool> isAuthenticated() async {
  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
      HeraRequest req = HeraRequest();
      req.cloudToken = await _authorize.getAccessToken();
      // set metadata for token
      Token _token = Token();
      _token.deviceInfo = await _getDeviceInfo();
      Placemark? _placemark = this._placemark ?? await _determinePlacemark();
      if (_placemark != null) {
        _token.loggedInFrom = _placemark.locality ?? "";
      }
      _token.refreshToken = await _getRefreshToken();
      req.token = _token;
      req.namespace = namespace;
      HeraResponse 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;
}