Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example Shiny app not working #63

Closed
bigbadcrad opened this issue Aug 21, 2023 · 0 comments · Fixed by #65
Closed

Example Shiny app not working #63

bigbadcrad opened this issue Aug 21, 2023 · 0 comments · Fixed by #65

Comments

@bigbadcrad
Copy link

Hello! I cannot get the example shiny app to work because I get a message that "HttpOnly cookies can only be updated via set_cookie_response()."

However, if I remove the .is_http_only() check from the set_cookie() function, then I can get the app to work (See below). I'm using R 4.0.3 on Windows and Firefox as my browser, but I also tried R 4.2.2 and Chrome as a browser and had the same problem.

library(cookies)
library(shiny)

set_cookie <- function(cookie_name, cookie_value, expiration = 90, secure_only = NULL,
                       domain = NULL, path = NULL, same_site = NULL, session = shiny::getDefaultReactiveDomain()) {
  # if (.is_http_only(cookie_name, session)) {
  #   cli::cli_abort(
  #     c(
  #       x = "Cannot update cookie {cookie_name}.",
  #       i = "HttpOnly cookies can only be updated via set_cookie_response()."
  #     ),
  #     class = "error_http_only_js"
  #   )
  # }

  attributes <- cookies:::.javascript_attributes(
    expiration = expiration,
    secure_only = secure_only, domain = domain, path = path,
    same_site = same_site
  )
  shiny::observeEvent(cookies:::.root_session(session)$input$cookie_set_error,
    cli::cli_abort(cookies:::.root_session(session)$input$cookie_set_error),
    ignoreInit = TRUE, once = TRUE
  )
  session$sendCustomMessage("cookie-set", list(
    name = cookie_name,
    value = cookie_value, attributes = attributes
  ))
}

# Wrap your ui with add_cookie_handlers() to enable cookies.
ui <- add_cookie_handlers(
  fluidPage(
    titlePanel("A Simple App"),
    fluidRow(
      sliderInput(
        "number_selector",
        label = paste(
          "Select a number.",
          "This selector sets the cookie value.",
          "It also initializes with the cookie value.",
          "Refresh to see it remembered.",
          sep = "\n"
        ),
        min = 1,
        max = 10,
        value = 1
      ),
      sliderInput(
        "number_selector_no_cookie",
        label = paste(
          "Select a number.",
          "This one is not connected to the cookie.",
          "When you refresh, it will always initialize to 1.",
          sep = "\n"
        ),
        min = 1,
        max = 10,
        value = 1
      )
    )
  )
)

server <- function(input, output, session) {
  # When the value changes, set a cookie.
  observeEvent(
    input$number_selector,
    {
      set_cookie(
        cookie_name = "selected_number",
        cookie_value = input$number_selector
      )
    }
  )
  
  # Initialize the top slider from any cookies that come in at the start.
  observeEvent(
    get_cookie("selected_number"),
    updateSliderInput(
      inputId = "number_selector",
      value = get_cookie("selected_number")
    ),
    once = TRUE
  )
}

shinyApp(
  ui,
  server,
  # Cookies only work in an actual browser.
  options = list(
    launch.browser = TRUE
  )
)
jonthegeek added a commit that referenced this issue Aug 26, 2023
Closes #63.

When I rewrite handling to store things in session$userData, make sure this is dealt with more cleanly. I *think* there's a javascript change needed to handle things completely correctly.
jonthegeek added a commit that referenced this issue Aug 26, 2023
Closes #63.

When I rewrite handling to store things in session$userData, make sure this is dealt with more cleanly. I *think* there's a javascript change needed to handle things completely correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant