我下面有一个使用用户身份验证逻辑的闪亮应用程序。如您所见,首先使用以下内容创建数据框:
# dataframe that holds usernames, passwords and other user data
user_base <- data.frame(
user = c("user1", "user2","user1"),
password = c("pass1", "pass2","pass1"),
permissions = c("admin", "standard","admin"),
name = c("User One", "User Two","User One"),
stringsAsFactors = FALSE)
在此数据框中,用户使用user
作为用户名,password
作为密码,以便登录该应用程序。如您所见,我可以登录到该应用程序中,并且仅使用user2
个凭据查看该表,因为user1
存在于数据框的两行中。如果我跳过第三行,它将起作用。我的目标是根据我将使用的用户名和密码对原始数据帧user_base
进行子集设置,但现在我可以看到,如果我有多个具有相同用户名的行,则该行不起作用。一个额外的问题是,是否可以跳过密码并允许用户仅使用用户名登录。
library(shiny)
library(shinyauthr)
library(shinyjs)
# dataframe that holds usernames, passwords and other user data
user_base <- data.frame(
user = c("user1", "user2","user1"),
password = c("pass1", "pass2","pass1"),
permissions = c("admin", "standard","admin"),
name = c("User One", "User Two","User One"),
stringsAsFactors = FALSE)
ui <- fluidPage(
# must turn shinyjs on
shinyjs::useShinyjs(),
# 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")
)
server <- function(input, output, session) {
# call the logout module with reactive trigger to hide/show
logout_init <- callModule(shinyauthr::logout,
id = "logout",
active = reactive(credentials()$user_auth))
# call login module supplying data frame, user and password cols
# and reactive trigger
credentials <- callModule(shinyauthr::login,
id = "login",
data = user_base,
user_col = user,
pwd_col = password,
log_out = reactive(logout_init()))
# pulls out the user information returned from login module
user_data <- reactive({credentials()$info})
output$user_table <- renderTable({
# use req to only render results when credentials()$user_auth is TRUE
req(credentials()$user_auth)
user_data()
})
}
shinyApp(ui = ui, server = server)