PaulC91 / shinyauthr

R package with shiny authentication modules
https://paulc91.github.io/shinyauthr/
Other
428 stars 81 forks source link

Logout doesn't clear dashboard (shinydashboard) #36

Closed M3IT closed 4 years ago

M3IT commented 4 years ago

Great package, it's been really useful - thanks!

On logout, whatever is visible in a dashboard body stays visible, and the login prompt simply pushes it down. Is there a way to intercept the logout and set to a blank tab - or a simpler option?

image

library(shiny)
library(shinyauthr)
library(shinyjs)
library(shinydashboard)

# dataframe that holds usernames, passwords and other user data
user_base <- data.frame(
  user = c("user1", "a"),
  password = c("pass1", ""),
  permissions = c("admin", "standard"),
  name = c("User One", "User Two"),
  stringsAsFactors = FALSE
)

header  <- dashboardHeader(title = 'Shiny Auth Test')
sidebar <- dashboardSidebar(
                 shinyauthr::logoutUI(id = 'logout', label = 'Log out', icon = icon('times-circle'))
                ,uiOutput('sidebarpanel')
                )
body    <- dashboardBody(
                 useShinyjs()
                ,shinyauthr::loginUI('login')
                ,uiOutput('body')
                )
ui      <- dashboardPage(header, sidebar, body, skin = 'purple')

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$sidebarpanel <- renderUI({
    req(credentials()$user_auth)
    div(
        sidebarMenu(id = "sidebar"
            ,menuItem('Chart',  tabName = 't_item1', icon = icon('chart-bar'))
            ,menuItem('Widget', tabName = 't_item2', icon = icon('ambulance'))
            ,menuItem(''      , tabName = 't_blank')
        )
    )
  })

  output$body <- renderUI({
    tabItems(
         tabItem(tabName = "t_item1", h2("Chart tab content"))
        ,tabItem(tabName = "t_item2", h2("Widget tab content"))
        ,tabItem(tabName = "t_blank", h2(''))
      )
  })
}

shinyApp(ui = ui, server = server)
PaulC91 commented 4 years ago

Hi, try adding req(credentials()$user_auth) inside your output$body render function like you have in the sidebarpanel render.

M3IT commented 4 years ago

Thanks Paul, that works. Pretty obvious really, can't believe I didn't see it.