0

Please see the code below. I met a problem when running a shiny app under a secure page. The "onSessionEnded" function is triggered once when I log into my account. And then it's triggered once again when I close the app.

Do you know how to make "onSessionEnded" not triggered when I log into the account? Ideally, "onSessionEnded" should only be triggered when the app is closed, right? Could anybody give me some suggestions?

You can run the code directly with username: a and password: a

library(shiny)
library(shinymanager)

credentials <- data.frame(
  user = c("a", "b"),
  password = c("a", "b"),
  stringsAsFactors = FALSE
)

ui <- fluidPage(
  verbatimTextOutput("auth_output")
)

ui <- secure_app(ui)

lang <- shinymanager:::language$new()
lang$add(
  "Please authenticate" = "Test Page",
  "Username:" = "Username:",
  "Password:" = "Password:",
  "Login" = "Sign in"
)

server <- function(input, output, session) {

  res_auth <- secure_server(
    check_credentials = check_credentials(credentials)
  )

  output$auth_output <- renderPrint({
    reactiveValuesToList(res_auth)
  })

  session$onSessionEnded(function() {
    print("onSessionEnded Run!!!")
  })

}

shinyApp(ui, server)

1 Answers1

1

I don't have the rep to comment on your post, but I believe this could be answered/addressed by this other stackoverflow post.

Shiny Server - how to use session$onSessionEnded()

As it should probably be implemented in your code:

library(shiny)
library(shinymanager)


credentials <- data.frame(
  user = c("a", "b"),
  password = c("a", "b"),
  stringsAsFactors = FALSE
)

ui <- fluidPage(
  verbatimTextOutput("auth_output")
)

ui <- secure_app(ui)

lang <- shinymanager:::language$new()
lang$add(
  "Please authenticate" = "Test Page",
  "Username:" = "Username:",
  "Password:" = "Password:",
  "Login" = "Sign in"
)

server <- function(input, output, session) {

  users_data <- data.frame(START = Sys.time())

  res_auth <- secure_server(
    check_credentials = check_credentials(credentials)
  )

  output$auth_output <- renderPrint({
    reactiveValuesToList(res_auth)
  })


  session$onSessionEnded(function() {
    users_data$END <- Sys.time()
  })

}

shinyApp(ui, server)

Hope this helps

Tim
  • 105
  • 7