datastorm-open / shinymanager

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

Is it possible to run code before authentication screen? #168

Closed mcsimenc closed 11 months ago

mcsimenc commented 12 months ago

I'd like to show a modal before the authentication screen. Is this possible? Thanks!

pvictor commented 11 months ago

Hello,

You have to decrease authentication panel's z-index so that the modal appear above it (or increase z-index of the modal) and then call showModal at the top level of your server. Here's an example:

library(shiny)
library(shinymanager)

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

# Wrap your UI with secure_app
ui <- secure_app(
  ui, 
  head_auth = tagList(
    tags$style(".panel-auth {z-index: 123 !important;}")
  )
)

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

  showModal(modalDialog("Welcome"))
  ## or you can display each time auth page is displayed (when disconnecting for example)
  # observeEvent(input$shinymanager_where, {
  #   if (identical(input$shinymanager_where, "authentication")) {
  #     showModal(modalDialog("Welcome"))
  #   }
  # })

  res_auth <- secure_server(
    check_credentials = check_credentials(data.frame(user = "user", password = "pwd"))
  )

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

  # your classic server logic

}

shinyApp(ui, server)
mcsimenc commented 11 months ago

Fantastic, thank you!