New password module in R Shinymanager not working

572 views Asked by At

I am trying to get the New password module of the R Shinymanager package to work in a R Shiny app but I can't seem to be able to store the new password anywhere. The function update_pwd in the code below should allow me to do this but I can't get it to work. I just need a way to store the new password (against the given username) and then update it in the credentials table.

Here is the code (based on https://rdrr.io/cran/shinymanager/man/module-password.html)

library(shinymanager)

if (interactive()) {

library(shiny)
library(shinymanager)

credentials <- data.frame(
user = c("test","manager"), # mandatory
password = c("test"), # mandatory
#start = c("2021-05-15","123456"), # optinal (all others)
#expire = c(NA, "2022-12-31"),
admin = c(FALSE, TRUE),
comment = "new user x",
stringsAsFactors = FALSE
)

ui <- fluidPage(
tags$h2("Change password module"),
actionButton(
inputId = "reset", label = "Reset password"
),
verbatimTextOutput(outputId = "res_pwd")
)


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


# adding UI to add user name 
observeEvent(input$reset, {
  # display a modal dialog to allow user to enter user name
  showModal(modalDialog(
    tags$h2('Please enter your user name'),
    textInput('username', 'User Name'),
    footer=tagList(
      actionButton('submit', 'Submit'),
      modalButton('cancel')
    )
  ))
})


### initiate password change
observeEvent(input$submit, {
  removeModal()
  insertUI(
    selector = "body",
    ui = tags$div(
      id = "module-pwd",
      pwd_ui(id = "pwd")
    )
  )
})



output$res_pwd <- renderPrint({
  reactiveValuesToList(pwd_out)
})


pwd_out <- callModule(
  module = pwd_server,
  id = "pwd",
  user = reactiveValues(user = input$username),
  #user = reactiveValues(user = "me"),
  update_pwd = function(user, pwd) {
    # store the password and user name somewhere
    list(results = TRUE)
  }
  )


observeEvent(pwd_out$relog, {
  removeUI(selector = "#module-pwd")
})
}

shinyApp(ui, server)
}
1

There are 1 answers

4
geotheory On

The stock example you're using doesn't dock into anything. You need to edit the line

# store the password and user name somewhere

to store pwd string somewhere - e.g. formal database or maybe just csv file for small number of users. I think it's good practice to store the passwords hashed - e.g. cli::hash_sha256(pwd).