2

I have put together a secure app to be hosted on shinyapps.io and everything works as desired except for my reactive server functions begin to run while waiting for credentials to be entered, and then run again once the credentials are verified. This seems like it would result in additional server time and resource use in the app. Is there a way to delay the server processes until after the credentials are entered. It is set up in the same manner as the example on the shinymanager vignettes:

library(shiny)
library(shinymanager)


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



ui <- fluidPage(
  tags$h2("My secure application"),
  
)

# Wrap your UI with secure_app
ui <- secure_app(ui)


server <- function(input, output, session) {
  
  # call the server part
  # check_credentials returns a function to authenticate users
  res_auth <- secure_server(
    check_credentials = check_credentials(credentials)
  )
  
  output$auth_output <- renderPrint({
    reactiveValuesToList(res_auth)
  })
  
  # all my server side functions here
  
}

shinyApp(ui, server)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
BEVAN
  • 426
  • 2
  • 12
  • Where is your data.frame with credentials info positioned? Could you please explain why you put credentials into server part? – TarJae Apr 04 '22 at 20:33
  • The credentials is supplied as a data frame before the ui and server portions, at the same point where necessary libraries are loaded. I put the credentials check in the server portion because that is how it was shown as the example in the vignette. When I run the app locally I can see that it needs to complete all the server functions (in this case querying telemetry data from providers API) before it allows credentials to be entered, and then runs it all again increasing the time and resources to start the app. – BEVAN Apr 04 '22 at 22:50
  • Edited post to have dummy example of where the credentials are inputted as dataframe – BEVAN Apr 04 '22 at 22:52

1 Answers1

3

We can use observeEvent, eventReactive or bindEvent to trigger functions in the server part after successful authentication:

library(shiny)
library(shinymanager)

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

ui <- fluidPage(
  tags$h2("My secure application"),
  verbatimTextOutput("auth_output"),
  plotOutput("myPlot")
)

# Wrap your UI with secure_app
ui <- secure_app(ui)


server <- function(input, output, session) {
  # call the server part
  # check_credentials returns a function to authenticate users
  res_auth <- secure_server(check_credentials = check_credentials(credentials))
  
  output$auth_output <- renderPrint({
    reactiveValuesToList(res_auth)
  })
  
  observeEvent(res_auth$user, {
    # all my server side functions here
    print("executing server functions...")
  })
}

shinyApp(ui, server)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78