Shiny authentication module to be used with signinUI. It uses shiny's new moduleServer method.

signinServer(
  id,
  users_db,
  sodium_hashed = TRUE,
  signout = shiny::reactiveVal(),
  reload_on_signout = FALSE,
  cookie_logins = FALSE,
  sessionid_col,
  cookie_getter,
  cookie_setter
)

Arguments

id

The ID for the server module.

users_db

data.frame or tibble containing five columns: date_created, username, password, name, email, and permissions. See create_dummy_users.

sodium_hashed

have the passwords been hash encrypted using the sodium package? defaults to TRUE.

signout

reactive supply the returned reactive from signoutServer here to trigger a user sign-out

reload_on_signout

logical to force a session reload on logout? defaults to FALSE.

cookie_logins

enable automatic logins via browser cookies?

sessionid_col

bare (unquoted) or quoted column name containing session ids

cookie_getter

a function that returns a data.frame with at least two columns: user and session

cookie_setter

a function with two parameters: user and session. The function must save these to a database.

Value

The module will return a reactive six element list to your main application.

  1. user_auth is a boolean indicating whether there has been a successful login or not.

  2. info will be the data frame provided to the function, filtered to the row matching the successfully logged in username. When user_auth is FALSE info is NULL.

  3. cookie_already_checked TRUE OR FALSE.

  4. users_db to be used in sign-up and password recovery features.

  5. btn_signup to be passed to sign-up module.

  6. btn_forgotpw to be passed to password recovery module.

Examples


library(shiny)
library(shinyAuthX)

# dataframe that holds usernames, passwords and other user data
users_base <- create_dummy_users()

ui <- fluidPage(
  # add signout button UI
  div(class = "pull-right", signoutUI(id = "signout")),

  # add signin panel UI function without signup or password recovery panel
  signinUI(id = "signin", .add_forgotpw = FALSE, .add_btn_signup = FALSE),

  # setup output to show user info after signin
  verbatimTextOutput("user_data")
)

server <- function(input, output, session) {
  # Export reactive values for testing
  exportTestValues(
    auth_status = credentials()$user_auth,
    auth_info   = credentials()$info
  )

  # call the signout module with reactive trigger to hide/show
  signout_init <- signoutServer(
    id = "signout",
    active = reactive(credentials()$user_auth)
  )

  # call signin module supplying data frame,
  credentials <- signinServer(
    id = "signin",
    users_db = users_base,
    sodium_hashed = TRUE,
    reload_on_signout = FALSE,
    signout = reactive(signout_init())
  )

  output$user_data <- renderPrint({
    # use req to only render results when credentials()$user_auth is TRUE
    req(credentials()$user_auth)
    str(credentials())
  })
}

if (interactive()) shinyApp(ui = ui, server = server)