Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow reactive user data #62

Merged
merged 2 commits into from
Feb 29, 2024
Merged

Conversation

burgerga
Copy link
Contributor

@burgerga burgerga commented Dec 15, 2022

This will allow updating the allowed users without restarting the app.
Backwards compatible with providing user_base as a tibble (meaning shinyauthr::runExample() still works).
Example usage using reactiveFileReader where usernames and passwords are stored using csv:

user_base.csv:

user,password,permissions,name
user1,pass1,admin,User One
user2,pass2,standard,User Two

app.R:

library(shiny)

user_base <- reactiveFileReader(1000, NULL, 'user_base.csv', read.csv)

ui <- fluidPage(
  # add logout button UI
  div(class = "pull-right", shinyauthr::logoutUI(id = "logout")),
  # add login panel UI function
  shinyauthr::loginUI(id = "login"),
  # setup table output to show user info after login
  tableOutput("user_table"),
  tableOutput("user_table2")
)

server <- function(input, output, session) {
  
  # call login module supplying data frame, 
  # user and password cols and reactive trigger
  credentials <- shinyauthr::loginServer(
    id = "login",
    data = user_base,
    user_col = user,
    pwd_col = password,
    log_out = reactive(logout_init())
  )
  
  # call the logout module with reactive trigger to hide/show
  logout_init <- shinyauthr::logoutServer(
    id = "logout",
    active = reactive(credentials()$user_auth)
  )
  
  output$user_table <- renderTable({
    # use req to only render results when credentials()$user_auth is TRUE
    req(credentials()$user_auth)
    credentials()$info
  })
  # DEBUG
  output$user_table2 <- renderTable({
    user_base()
  })
}

shinyApp(ui = ui, server = server)

@PaulC91 PaulC91 merged commit 480b83f into PaulC91:master Feb 29, 2024
1 of 5 checks passed
@PaulC91
Copy link
Owner

PaulC91 commented Feb 29, 2024

Thanks for this PR, it's a very useful feature. And sorry it took so long to merge!

@burgerga burgerga deleted the feat_reactive_userdata branch February 29, 2024 18:45
@burgerga
Copy link
Contributor Author

No problem :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants