Skip to content

Instantly share code, notes, and snippets.

@PaulC91
Last active April 24, 2023 18:47
Show Gist options
  • Save PaulC91/2a1b9ba57a53869ba82093720a9a8fa0 to your computer and use it in GitHub Desktop.
Save PaulC91/2a1b9ba57a53869ba82093720a9a8fa0 to your computer and use it in GitHub Desktop.
Example of how to use shinyauthr with a shiny navbarPage UI
library(shiny)
library(shinyauthr)
library(shinyjs)
user_base <- data.frame(
user = c("user1", "user2"),
password = c("pass1", "pass2"),
permissions = c("admin", "standard"),
name = c("User One", "User Two"),
stringsAsFactors = FALSE
)
ui <- tagList(
shinyjs::useShinyjs(),
tags$head(includeScript("logout-button.js")),
navbarPage(
id = "tabs",
title = "shinyauthr with Navbar Page",
tabPanel("Home", loginUI("login"), uiOutput("homepage")),
tabPanel("Data", tableOutput("tbl"))
)
)
server <- function(input, output, session) {
# go back to the home tab after logout
observeEvent(credentials()$user_auth, {
updateNavbarPage(session, "tabs", selected = "Home")
})
# call the logout module with reactive trigger to hide/show
logout_init <- callModule(logout,
id = "logout",
active = reactive(credentials()$user_auth))
# call login module supplying data frame, user and password cols
# and reactive trigger
credentials <- callModule(login,
id = "login",
data = user_base,
user_col = user,
pwd_col = password,
log_out = reactive(logout_init()))
output$homepage <- renderUI({
req(credentials()$user_auth)
tags$h1("Something in the homepage")
})
output$tbl <- renderTable({
req(credentials()$user_auth)
credentials()$info
})
}
shinyApp(ui, server)
$(document).ready(function() {
$(".navbar .container-fluid").append('<div class="pull-right"><button id="logout-button" class="btn navbar-btn action-button btn-danger shiny-bound-input" style="color: white; display: none;" type="button">Log out</button></div>');
});
@PaulC91
Copy link
Author

PaulC91 commented Jul 18, 2019

glad to hear you found the solution you needed!

@brianmsm
Copy link

brianmsm commented May 7, 2021

If anyone wants to know where the answer to the question is, it's here: https://community.rstudio.com/t/shinyauthr-with-shinydashboard-layout/30501/4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment