0

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)
}
Tomar
  • 11

1 Answers1

0

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).

geotheory
  • 22,624
  • 29
  • 119
  • 196
  • Thanks, I understand that I need to edit the line but I just can't get it to work to change the password in the data frame called "credentials" (see code snippet). This eventually will be a postgreSQL table but for now I am just testing it on a simple data frame. – Tomar Mar 14 '22 at 08:32
  • Something like `credentials$password[match(input$user, credentials$user)] <- input$pwd` might work. – geotheory Mar 14 '22 at 17:50
  • So I tried this already but it didn't seem to update the password in the credential data frame. – Tomar Mar 16 '22 at 12:24
  • 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 credentials$password[match(input$user, credentials$user)] <- input$pwd list(results = TRUE) } ) – Tomar Mar 16 '22 at 12:26