I was able to setup a shiny app password following this solution: Starting Shiny app after password input (with Shinydashboard)
Now, I just had contact with shiny modules, so I started wondering if it is possible to use modules to make a easy-to-setup password module that I could implement in other apps that I own. (The reason I'm using a module is only learn about it. This could be done using regular functions).
This is my current working solution:
##This part of the module is going to set modal's UI
passwordModuleUI=function(id,failed=FALSE){
ns=NS(id)
tagList(
modalDialog(
textInput(ns("username"), "Usuário:"),
passwordInput(ns("password"), "Senha:"),
footer = tagList(
# modalButton("Cancel"),
actionButton(ns("ok"), "OK")
)
)
)
}
#This will load modal UI from observer inside the server
passwordModuleShow=function(ID){
showModal(passwordModuleUI(ID))
}
#This is server side module
passwordModuleEval=function(input,output,session,id,users,passwords){
authenticated <- FALSE
isolate({
Username <- input$username
Password <- input$password
})
Id.username <- which(users == Username)
Id.password <- grep(Password,passwords)
if ((length(Id.username) > 0&nchar(Password)>1) & (length(Id.password) > 0)&nchar(Username)>1) {
if (Id.username %in% Id.password) {
authenticated <- TRUE
removeModal()
} else {
authenticated <- FALSE
}
}
return(authenticated)
}
#This is trying to simplify implementation in shiny apps
setupPassProtection=function(input,output,session,ID,Users,Passwords){
obs1=observe(priority = 1,{
passwordModuleShow(ID=ID)
})
observeEvent(input[[paste0(ID,"ok",sep="-")]],{
if(callModule(passwordModuleEval,ID,users=Users,passwords=Passwords))
{obs1$suspend()}
})
}
my_username=my_password="test"
ui=basicPage(h3("testando passprotect module"))
server=function(input,output,session){
passID="test"
obs1=observe(priority = 1,{
passwordModuleShow(ID=passID)
})
observeEvent(input[[paste0(passID,"-ok")]],{
if(callModule(passwordModuleEval,"test",users=my_username,passwords=my_password))
{obs1$suspend()}
})
}
shinyApp(ui,server)
I'd like to make the following SERVER work since it's the simplest more readable version, and I could easily source the other functions.
This is NOT working:
server=function(input,output,session){
setupPassProtection(input,output,session,ID="test",Users=my_username,Passwords=my_password)
}
Appreciate any help with the solution or code simplification.