datastorm-open / shinymanager

Simple and secure authentification mechanism for single shiny applications.
https://datastorm-open.github.io/shinymanager/
379 stars 79 forks source link

Using shinymanager along with session$OnSessionEnded() to close the app. #159

Open SanthoshManikantan opened 1 year ago

SanthoshManikantan commented 1 year ago

I have an application which uses the shinymanager library for user login. I want to use the session$onSessionEnded() to close my app once the browser is closed. As suggested by @ismirsehregal here shinymanager with onsessioneneded() the workaround is working fine for the login but if we logout or going to administrator mode (while using db) the app is getting stopped. Could you please provide a workaround for this. samplecode

`library(shiny) library(shinyWidgets) library(shinythemes) library(shinymanager)

credentials <- data.frame( user = c("admin", "shinymanager"), # mandatory password = c("admin", "12345"), # mandatory start = c("2022-04-15"), # optinal (all others) expire = c(NA, "2026-12-31"), admin = c(TRUE, TRUE), comment = "Simple and secure authentification mechanism for single ‘Shiny’ applications.", stringsAsFactors = FALSE )

ui <- fluidPage( sliderInput("n", "Number of observations", 2, 1000, 500), plotOutput("plot") )

ui <- secure_app(ui)

server <- function(input, output, session) { res_auth <- secure_server( check_credentials = check_credentials(credentials) )

session$onSessionEnded(function() { print(paste("Session", session$token, "ended")) if(!is.null(isolate({res_auth$user}))){ stopApp() } })

observe({

Re-execute this reactive expression after 1000 milliseconds

invalidateLater(1000, session)
print(paste("The value of input$n is", isolate(input$n)))

}) output$plot <- renderPlot({

Re-execute this reactive expression after 2000 milliseconds

invalidateLater(2000)
hist(rnorm(isolate(input$n)))

}) }

shinyApp(ui, server)`

SEvisual commented 4 months ago

I've encountered the same issue and hope to find a solution!

pvictor commented 4 months ago

Why do you need to call stopApp() ? stopApp() is useful in local to free the R session but has no interest afaik on a server

SEvisual commented 4 months ago

such as using shinymanager with DesktopDeployR(A framework for deploying self-contained R-based applications to the desktop), session$onSessionEnded(function() {stopApp()}) IMPORTANT! this is needed to terminate the R process when the shiny app session ends. Otherwise, you end up with a zombie process