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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading