PaulC91 / shinyauthr

R package with shiny authentication modules
https://paulc91.github.io/shinyauthr/
Other
427 stars 81 forks source link

Change password #24

Closed AndreasPhilippi closed 5 years ago

AndreasPhilippi commented 5 years ago

Hi, I'm trying to change the password during the run-time. Therefore i have a input-field (id=newPW) where the User can enter his new password and a Button to confirm (id=changePW). I try to change it by the following code:

observeEvent(input$changePW,{
credentials()$info$password <- input$newPW })

Unfortunately I'm always getting the following error: Warning: Error in <-: invalid (NULL) left side of assignment

Does anybody have an Idee how to change the data that is stored in credentials?

PaulC91 commented 5 years ago

You can't overwrite data inside a reactive like that. But you wouldn't want to because this would only change the password for the duration of their session then it would revert to whatever the password was in the user base your app reads in.

If you want this to be a permanent change you'll need to store your user base data in some form of remote data storage system (sql db, google sheet etc) that is readable and writable from your app. You could then have code that overwrites the pwd for that user on the database at their request, so when they next login and the app reads in your user base data with updated passwords.

AndreasPhilippi commented 5 years ago

Hi Paul, thanks for your answer and sry for the missing code-formating in the previous comment. I have my user data in a database on my server and I can change the password out of the UI. My only problem is that I'm not able to update the user base during the session. I also tried to make the user base reactive as you can see in the following code snipe. But that also dosen't work out for me. Is there any way to get that done?


# initialise pool objekt when running the App
pool <<- dbPool(
    drv = RMySQL::MySQL(),
    dbname = "mydb",
    host = "XXXXXXX",
    username = "shinyApp",
    password = "XXXXX")

# Server Code snipe
values <- reactiveValues()
values$trigger_user_base = TRUE

# trigger user_base with reactiveValue to get it initialised
user_base <- reactive({
  if(values$trigger_user_base){
    #read user data from database 
    pool%>%
      dbReadTable('user_data')%>%
      select(user,password,permissions,name)
      values$trigger_user_base = FALSE
  }
})

credentials <- callModule(shinyauthr::login, 
                          id = "login", 
                          data = reactive(user_base()),
                          user_col = user,
                          pwd_col = password,
                          log_out = reactive(logout_init())

observeEvent(input$changePW,{
  #change pw in database
  sql <- "UPDATE login_data SET password=?password WHERE name=credentials()$info$name";
  query <- sqlInterpolate(pool, sql, password = input$neupasswort1)
  dbGetQuery(pool, query)

  # trigger user_base
  values$trigger_user_base <- TRUE
})
)```
KryeKuzhinieri commented 2 years ago

Is there any solution to this? Is it possible to update the user base table within the session without having to restart it?

PaulC91 commented 2 years ago

No, shinyauthr::loginServer (previously shinyauthr::login) won't work with a reactive data object, so you'd need to load your user base inside your server function and then reload the session after the user base has changed to retrieve the updated user base.

You could build this into a change password process by adding session$reload() at the end. This will log out the user and they can then log in again with their new password, which I think is quite a normal UX procedure for password changes.

KryeKuzhinieri commented 2 years ago

Yes, that's what I ended up doing. Thanks for the response!