简体   繁体   中英

Shiny dashboard with user authentication

I am making a shiny app which will show a dashboard but before that, it will ask for user login. There are two UIs, ui_login and ui_app, But in the ui_app if I use fluidPage/fixedPage or some other instead of dashboardPage it's working fine. Is there any way that I can show the dashboardPage after login.

library(shiny)
library(shinydashboard)

my_username = "user"
my_password = "pass"

ui <- uiOutput("page")

ui_login <- fluidPage(
  div(
    textInput("username", "Username"),
    passwordInput("passwd", "Password"),
    br(),
    actionButton("Login", "Log In"),
    uiOutput("invalid")
  )
)

ui_app <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody()
)

server <- function(input, output, session) {
  #app server code-----

  observe({
    if(USER$Logged == FALSE) {
      if(!is.null(input$Login)) {
        if(input$Login > 0) {
          Username <- isolate(input$username)
          Password <- isolate(input$passwd)
          if((Username == my_username) & (Password == my_password)){
            USER$Logged = TRUE
          }
          else {
            output$invalid <- renderUI(h4("Please enter valid login credentials!", style="color:red;"))
          }
        }
      }
    }
    else {
      output$invalid <- renderUI(h4("Please enter valid login credentials!", style="color:red;"))
    }
  })
  observe({
    if(USER$Logged == FALSE) {
      output$page <- renderUI(ui_login) 
    }
    else {
      output$page <- renderUI(ui_app) 
    }
  })
}
shinyApp(ui, server)

I recently wrote an R package that provides login/logout modules you can integrate with shinydashboard.

Example app

Package repo

the inst/ directory in the package repo contains the code for the example app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM