I have a shiny app where I am using the awesome shinyauthr package. However, I am having one issue. There is a blank page once I sign in.
Right now I have 5 tabs in my sidebar. In my UI I access the information in each sidebar with tabItems().
shinyauthr does not automatically open up the tab. Instead I have to open a new tab and then go back to the original tab to display the data.
Here is the graphical issue before I show the code.
Here is my code. I think I have to add some java.script somewhere that says, open the first tab item after authentication.
The full code is 700 lines so I will just post a snippet of the code. If you would like me to show all my code, I will be happy to post it.
***I have edited the code to give a reproducible example.
library(shiny)
library(shinydashboard)
library(dplyr)
library(shinyjs)
library(glue)
library(shinyauthr)
user_base <- data_frame(
user = c("user1", "user2"),
password = c("pass1", "pass2"),
password_hash = sapply(c("pass1", "pass2"), sodium::password_store),
permissions = c("admin", "standard"),
name = c("User One", "User Two")
)
ui <- dashboardPage(
dashboardHeader(title = "shinyauthr",
tags$li(class = "dropdown", style = "padding: 8px;",
shinyauthr::logoutUI("logout")),
tags$li(class = "dropdown",
tags$a(icon("github"),
href = "https://github.com/paulc91/shinyauthr",
title = "See the code on github"))
),
dashboardSidebar(collapsed = TRUE,
sidebarMenu(
menuItem("dash", tabName = "dash"),
menuItem("hey", tabName = "hey")
)
),
dashboardBody(
shinyjs::useShinyjs(),
tags$head(tags$style(".table{margin: 0 auto;}"),
tags$script(src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/3.5.16/iframeResizer.contentWindow.min.js",
type="text/javascript"),
includeScript("returnClick.js")
),
shinyauthr::loginUI("login"),
uiOutput("user_table"),
uiOutput("testUI"),
HTML('<div data-iframe-height></div>')
)
)
server <- function(input, output, session) {
credentials <- callModule(shinyauthr::login, "login",
data = user_base,
user_col = user,
pwd_col = password_hash,
sodium_hashed = TRUE,
log_out = reactive(logout_init()))
logout_init <- callModule(shinyauthr::logout, "logout", reactive(credentials()$user_auth))
observe({
if(credentials()$user_auth) {
shinyjs::removeClass(selector = "body", class = "sidebar-collapse")
} else {
shinyjs::addClass(selector = "body", class = "sidebar-collapse")
}
})
output$user_table <- renderUI({
# only show pre-login
if(credentials()$user_auth) return(NULL)
tagList(
tags$p("test the different outputs from the sample logins below
as well as an invalid login attempt.", class = "text-center"),
renderTable({user_base[, -3]})
)
})
user_info <- reactive({credentials()$info})
user_data <- reactive({
req(credentials()$user_auth)
if (user_info()$permissions == "admin") {
dplyr::starwars[,1:10]
} else if (user_info()$permissions == "standard") {
dplyr::storms[,1:11]
}
})
output$welcome <- renderText({
req(credentials()$user_auth)
glue("Welcome {user_info()$name}")
})
x = as.data.frame(rnorm(100))
output$testUI <- renderUI({
req(credentials()$user_auth)
tabItems(
tabItem(tabName = "dash",
fluidRow(
column(
width = 12,
tags$h2(glue("Your permission level is: {user_info()$permissions}.
Your data is: {ifelse(user_info()$permissions == 'admin', 'Starwars', 'Storms')}.")),
box(width = NULL, status = "primary",
title = ifelse(user_info()$permissions == 'admin', "Starwars Data", "Storms Data"),
DT::renderDT(user_data(), options = list(scrollX = TRUE))
)
)
)),
tabItem(tabName = "hey",
DT::renderDataTable(x)
)
)
})
}
shiny::shinyApp(ui, server)
You have to create a .js file in the same folder called returnClick.js
$(document).keyup(function(event) {
if ($("#login-password").is(":focus") && (event.keyCode == 13)) {
$("#login-button").click();
}
});