shinyworks / cookies

Use Browser Cookies with 'shiny'
https://shinyworks.github.io/cookies/
Other
32 stars 4 forks source link

Example Shiny app not working #63

Closed bigbadcrad closed 1 year ago

bigbadcrad commented 1 year ago

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
  )
)