0

Is there a simple way for me to create a log-in page for my app? I want to render a ui with multiple tabs (ie. fluidPage(navbarPage(tabPanel("tab1", ...), tabPanel("tab2", ...),...)) conditional upon successful user authentication. I initiate a database connection with an R6 object, if it fails, I print out an "error" and the user can retry entering their credentials. If the connection is successful, I'd like to render the ui (ui2) I specify above.

ui <- fluidPage(fluidRow(column(width=12, textInput('uid', label =                
                        "Enter your user id:"), passwordInput("pwd",
                        label = "Enter your password:"), actionButton("login", 
                        "Login"), uiOutput("resulting_ui"), offset = 5)))
massisenergy
  • 1,764
  • 3
  • 14
  • 25
Lorenzo M
  • 88
  • 7
  • I tried using `output$resulting_ui <- renderUI({ui2})`. I haven't tried this, because I'm not sure how to do it, but perhaps I could use a `conditionalPanel(condition == "!is.null(dbConn)")` But I'm not sure what the javascript code for the condition would look like. – Lorenzo M Dec 05 '18 at 21:35
  • Maybe I could do this with shinyjs? – Lorenzo M Dec 05 '18 at 22:18
  • You could hide ui elements such as the tabs with shinyjs, and the use an `observeEvent` to monitor the submission of credentials. When the user submits, simple `if(input$uid=="Joe1" & input$pwd=="Password123") { updateTabsetPanel(session, tab1)....shinyjs(show: tab1)...}' And then use a shiny alert otherwise? 'else( shinyalert("invalid)...` Admittedly If you have a lot of users this will not be very efficient. – Chabo Dec 05 '18 at 23:07
  • 1
    Also note I have no idea how secure this will be. – Chabo Dec 05 '18 at 23:10
  • 3
    Possible duplicate of [Starting Shiny app after password input](https://stackoverflow.com/questions/28987622/starting-shiny-app-after-password-input) – ismirsehregal Dec 06 '18 at 05:57
  • Please have a look at library([shinyauthr](https://github.com/paulc91/shinyauthr)). Here is an [example](https://cultureofinsight.shinyapps.io/shinyauthr/) using shinydashboard. – ismirsehregal Dec 06 '18 at 08:07

2 Answers2

1

If you already set up a database that can hold usernames and passwords, you can have users enter their credentials, compare them to the ones in the database, and change a value of a reactiveVal() based on that comparison. The value of that reactiveVal() will control whether you show your UI or not.

For example you can use something like this:

logged <- reactiveVal(value = F)

# change the value of logged() when a user enters correct credentials

output$your_ui <- renderUI({
   req(logged())
   # your ui here...
})

For a more elaborated example take a look here:
https://github.com/yanirmor/shiny-user-management

Yanir Mor
  • 581
  • 3
  • 15
1

Starting Shiny app after password input This did mostly resolve my question. I just had to make some minor changes to get it to work.

Lorenzo M
  • 88
  • 7