PaulC91 / shinyauthr

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

Add additional condition to login (along user and pass) #26

Closed MislavSag closed 5 years ago

MislavSag commented 5 years ago

Along user and pass, Is it possible to add additional condition to enter shiny app?

More concretely, I would like to add date condition, that is date column for specific user have to be grater then today.

Something like this (server part):

  credentials <- callModule(shinyauthr::login, "login", 
                            data = user_base,
                            user_col = user,
                            pwd_col = password_hash,
                            sodium_hashed = TRUE,
                            log_out = reactive(logout_init()))

  logout_init <- callModule(shinyauthr::logout, "logout", reactive(credentials()$user_auth))

  observe({
    if(credentials()$user_auth) { #  & user_info$enddate > Sys.Date() 
      shinyjs::removeClass(selector = "body", class = "sidebar-collapse")
    } else {
      shinyjs::addClass(selector = "body", class = "sidebar-collapse")
    }
  })

  user_info <- reactive({credentials()$info})

  output$welcome <- renderText({
    req(credentials()$user_auth)

    glue("{user_info()$name}")
  })

Please look at observe part. This code doesn't work, but demonstrate what I want.

PaulC91 commented 5 years ago

Yes you can just replace all the conditions that wait for credentials()$user_auth to be TRUE before activating with your own custom condition that included the date predicate.

credentials <- callModule(shinyauthr::login, "login", 
                          data = user_base,
                          user_col = user,
                          pwd_col = password_hash,
                          sodium_hashed = TRUE,
                          log_out = reactive(logout_init()))

logout_init <- callModule(shinyauthr::logout, "logout", reactive(credentials()$user_auth))

user_info <- reactive({credentials()$info})

my_condition <- reactive({
  # all returns TRUE only if both conditions are TRUE
  all(credentials()$user_auth, user_info()$enddate > Sys.Date())
})

observe({
  if(my_condition()) { 
    shinyjs::removeClass(selector = "body", class = "sidebar-collapse")
  } else {
    shinyjs::addClass(selector = "body", class = "sidebar-collapse")
  }
})

output$welcome <- renderText({
  req(my_condition())

  glue("{user_info()$name}")
})
MislavSag commented 5 years ago

It works. Thanks!