Skip to content

Commit

Permalink
Merge pull request #62 from burgerga/feat_reactive_userdata
Browse files Browse the repository at this point in the history
Allow reactive user data
  • Loading branch information
PaulC91 authored Feb 29, 2024
2 parents c6537bb + 3c63399 commit 480b83f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Suggests:
knitr,
rmarkdown,
covr
RoxygenNote: 7.1.1
RoxygenNote: 7.2.3
URL: https://github.com/paulc91/shinyauthr
BugReports: https://github.com/paulc91/shinyauthr/issues
Config/testthat/edition: 3
19 changes: 12 additions & 7 deletions R/login.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ loginUI <- function(id,
#' \href{https://shiny.rstudio.com/articles/modules.html}{Modularizing Shiny app code}.
#'
#' @param id An ID string that corresponds with the ID used to call the module's UI function
#' @param data data frame or tibble containing user names, passwords and other user data
#' @param data data frame or tibble containing user names, passwords and other user data. Can be either
#' a static object or a shiny \link[shiny]{reactive} object
#' @param user_col bare (unquoted) or quoted column name containing user names
#' @param pwd_col bare (unquoted) or quoted column name containing passwords
#' @param sodium_hashed have the passwords been hash encrypted using the sodium package? defaults to FALSE
Expand Down Expand Up @@ -121,8 +122,12 @@ loginServer <- function(id,
}
}

# ensure all text columns are character class
data <- dplyr::mutate_if(data, is.factor, as.character)
data_reactive <- shiny::reactive({
# if not already reactive, make data reactive
data_temp <- if (shiny::is.reactive(data)) data else shiny::reactive(data)
# ensure all text columns are character class
dplyr::mutate_if(data_temp(), is.factor, as.character)
})

shiny::moduleServer(
id,
Expand Down Expand Up @@ -191,7 +196,7 @@ loginServer <- function(id,

credentials$user_auth <- TRUE
credentials$info <- dplyr::bind_cols(
dplyr::filter(data, {{user_col}} == .userid),
dplyr::filter(data_reactive(), {{user_col}} == .userid),
dplyr::select(cookie_data, -{{user_col}})
)
}
Expand All @@ -203,10 +208,10 @@ loginServer <- function(id,
shiny::observeEvent(input$button, {

# check for match of input username to username column in data
row_username <- which(dplyr::pull(data, {{user_col}}) == input$user_name)
row_username <- which(dplyr::pull(data_reactive(), {{user_col}}) == input$user_name)

if (length(row_username)) {
row_password <- dplyr::filter(data, dplyr::row_number() == row_username)
row_password <- dplyr::filter(data_reactive(), dplyr::row_number() == row_username)
row_password <- dplyr::pull(row_password, {{pwd_col}})
if (sodium_hashed) {
password_match <- sodium::password_verify(row_password, input$password)
Expand All @@ -221,7 +226,7 @@ loginServer <- function(id,
if (length(row_username) == 1 && password_match) {

credentials$user_auth <- TRUE
credentials$info <- dplyr::filter(data, {{user_col}} == input$user_name)
credentials$info <- dplyr::filter(data_reactive(), {{user_col}} == input$user_name)

if (cookie_logins) {
.sessionid <- randomString()
Expand Down
3 changes: 2 additions & 1 deletion man/loginServer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 480b83f

Please sign in to comment.