datastorm-open / shinymanager

Simple and secure authentification mechanism for single shiny applications.
https://datastorm-open.github.io/shinymanager/
386 stars 79 forks source link

Could I use auth_ui with Shinydashboard? #83

Closed Raymond-KT closed 3 years ago

Raymond-KT commented 3 years ago

I want to use auth_ui background with shinydashboard. But It's not working.

when I use auth_ui with ui<- fluidepage(), It's working. But It's not working with ui<-dashboardpage()

Is there a solution to custom auth_ui with shinydashboard?

credentials <- data.frame( user = c("####", "###"), password = c("####", "####"), admin = c(T, F), stringsAsFactors = FALSE)

ui <- dashboardPage(skin='black',

                auth_ui(
                  id='auth',
                  background  = "linear-gradient(rgba(0, 0, 255, 0.5),
                   rgba(255, 255, 0, 0.5)),
                   url('https://cdn.pixabay.com/photo/2016/02/19/11/19/office-1209640_1280.jpg');"),

  dashboardHeader(),
dashboardSidebar(),

. . . )

server <- function(input, output, session){

res_auth <- secure_server( check_credentials = check_credentials(credentials)) . . . }

Raymond-KT commented 3 years ago

this is the comment about the error

tagAssert(header, type = "header", class = "main-header")

pvictor commented 3 years ago

Hello, try calling auth_ui module somewhere in dashboardBody.

V

Raymond-KT commented 3 years ago

Hello!! thank you for your kindness, but I can't understand your advie TT

here is a simple example, could you explain again with this code?

library(shiny) library(shinymanager) library(shinydashboard)

credentials <- data.frame( user = c("fanny", "victor"), password = c(scrypt::hashPassword("azerty"), scrypt::hashPassword("12345")), is_hashed_password = TRUE, comment = c("alsace", "auvergne"), stringsAsFactors = FALSE )

ui <- dashboardPage( auth_ui( id = "auth",

add image on top ?

tags_top =
  tags$div(
    tags$h4("Demo", style = "align:center"),
    tags$img(
      src = "https://www.r-project.org/logo/Rlogo.png", width = 100
    )
  ),
# add information on bottom ?
tags_bottom = tags$div(
  tags$p(
    "For any question, please  contact ",
    tags$a(
      href = "mailto:someone@example.com?Subject=Shiny%20aManager",
      target="_top", "administrator"
    )
  )
),
# change auth ui background ?
# https://developer.mozilla.org/fr/docs/Web/CSS/background
background  = "linear-gradient(rgba(0, 0, 255, 0.5),
                   rgba(255, 255, 0, 0.5)),
                   url('https://www.r-project.org/logo/Rlogo.png');",
choose_language = TRUE

),

dashboardHeader(), dashboardSidebar(), dashboardBody() )

server <- function(input, output) {

res_auth <- secure_server( check_credentials = check_credentials(credentials))

}

ui <- secure_app(ui)

shinyApp(ui, server)

Raymond-KT commented 3 years ago

I get an error code like this.

Error in tagAssert(header, type = "header", class = "main-header") : Expected an object with class 'shiny.tag'.

pvictor commented 3 years ago

dashboardPage has a fixed number of arguments, first one must be a dashboardHeader, second one a dashboardSidebar, etc.. So you cannot use auth_ui here.

But if you use secure_app you do not need auth_ui at all :

library(shiny)
library(shinymanager)
library(shinydashboard)

credentials <- data.frame(
  user = c("fanny", "victor"),
  password = c(scrypt::hashPassword("azerty"), scrypt::hashPassword("12345")),
  is_hashed_password = TRUE,
  comment = c("alsace", "auvergne"),
  stringsAsFactors = FALSE
)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    tags$div("Normal body content")
  )
)

server <- function(input, output) {

  res_auth <- secure_server(
    check_credentials = check_credentials(credentials))

}

ui <- secure_app(ui)

shinyApp(ui, server)

auth_ui is just a module displaying an interface with login prompt, it's included in secure_app which contain the mechanism to secure access to the application. You can still use auth_ui in an application but dont do it with secure_app, otherwise you'll be asking user to enter user/pwd twice.

Raymond-KT commented 3 years ago

I'm really thank you for your kindness!!!! TT

actually, I want to custom displaying an interface with login prompt through auth_ui,

I'll try to use auth_ui without secure_app!!!

pvictor commented 3 years ago

In that case you can try:

library(shiny)
library(shinymanager)
library(shinydashboard)

credentials <- data.frame(
  user = c("fanny", "victor"),
  password = c(scrypt::hashPassword("azerty"), scrypt::hashPassword("12345")),
  is_hashed_password = TRUE,
  comment = c("alsace", "auvergne"),
  stringsAsFactors = FALSE
)

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

    auth_ui(
      id = "auth",
      # add image on top ?
      tags_top = 
        tags$div(
          tags$h4("Demo", style = "align:center"),
          tags$img(
            src = "https://www.r-project.org/logo/Rlogo.png", width = 100
          )
        ),
      # add information on bottom ?
      tags_bottom = tags$div(
        tags$p(
          "For any question, please  contact ",
          tags$a(
            href = "mailto:someone@example.com?Subject=Shiny%20aManager",
            target="_top", "administrator"
          )
        )
      )
    ),

    tags$div("Normal body content")
  )
)

server <- function(input, output) {

  auth <- callModule(
    module = auth_server,
    id = "auth",
    check_credentials = check_credentials(credentials)
  )

}

shinyApp(ui, server)
Raymond-KT commented 3 years ago

Oh!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I spend a lot of time for fix this problem!!!

I owe you my life!!!!!

thank you thank you!!

I just pressed the star button of your repository!!!