Title: | 'Shiny' Authentication Modules |
---|---|
Description: | Add in-app user authentication to 'shiny', allowing you to secure publicly hosted apps and build dynamic user interfaces from user information. |
Authors: | Paul Campbell [aut, cre]
|
Maintainer: | Paul Campbell <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.0 |
Built: | 2024-08-19 06:26:44 UTC |
Source: | CRAN |
Deprecated. Use loginServer instead.
input |
shiny input |
output |
shiny output |
session |
shiny session |
data |
data frame or tibble containing usernames, passwords and other user data |
user_col |
bare (unquoted) column name containing usernames |
pwd_col |
bare (unquoted) column name containing passwords |
sodium_hashed |
have the passwords been hash encrypted using the sodium package? defaults to FALSE |
hashed |
Deprecated. shinyauthr now uses the sodium package for password hashing and decryption. If you have previously hashed your passwords with the digest package to use with shinyauthr please re-hash them with sodium for decryption to work. |
algo |
Deprecated |
log_out |
[reactive] supply the returned reactive from logout here to trigger a user logout |
sessionid_col |
bare (unquoted) 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. |
reload_on_logout |
should app force reload on logout? |
Shiny authentication module for use with loginUI
Call via shiny::callModule(shinyauthr::login, "id", ...)
This function is now deprecated in favour of loginServer which uses shiny's new moduleServer method as opposed to the callModule method used by this function. See the loginServer documentation For details on how to migrate.
The module will return a reactive 2 element list to your main application.
First element user_auth
is a boolean indicating whether there has been
a successful login or not. Second element 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.
## Not run: user_credentials <- shiny::callModule( login, id = "login", data = user_base, user_col = user, pwd_col = password, log_out = reactive(logout_init()) ) ## End(Not run)
## Not run: user_credentials <- shiny::callModule( login, id = "login", data = user_base, user_col = user, pwd_col = password, log_out = reactive(logout_init()) ) ## End(Not run)
Shiny authentication module for use with loginUI
loginServer( id, data, user_col, pwd_col, sodium_hashed = FALSE, log_out = shiny::reactiveVal(), reload_on_logout = FALSE, cookie_logins = FALSE, sessionid_col, cookie_getter, cookie_setter )
loginServer( id, data, user_col, pwd_col, sodium_hashed = FALSE, log_out = shiny::reactiveVal(), reload_on_logout = FALSE, cookie_logins = FALSE, sessionid_col, cookie_getter, cookie_setter )
id |
An ID string that corresponds with the ID used to call the module's UI function |
data |
data frame or tibble containing user names, passwords and other user data |
user_col |
bare (unquoted) or quoted column name containing user names |
pwd_col |
bare (unquoted) or quoted column name containing passwords |
sodium_hashed |
have the passwords been hash encrypted using the sodium package? defaults to FALSE |
log_out |
[reactive] supply the returned reactive from logoutServer here to trigger a user logout |
reload_on_logout |
should app force a session reload on logout? |
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. |
This module uses shiny's new moduleServer method as opposed to the callModule method used by the now deprecated login function and must be called differently in your app. For details on how to migrate see the 'Migrating from callModule to moduleServer' section of Modularizing Shiny app code.
The module will return a reactive 2 element list to your main application.
First element user_auth
is a boolean indicating whether there has been
a successful login or not. Second element 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.
library(shiny) # dataframe that holds usernames, passwords and other user data user_base <- dplyr::tibble( user = c("user1", "user2"), password = c("pass1", "pass2"), permissions = c("admin", "standard"), name = c("User One", "User Two") ) ui <- fluidPage( # add logout button UI div(class = "pull-right", shinyauthr::logoutUI(id = "logout")), # add login panel UI function shinyauthr::loginUI(id = "login"), # setup table output to show user info after login tableOutput("user_table") ) server <- function(input, output, session) { # call login module supplying data frame, # user and password cols and reactive trigger credentials <- shinyauthr::loginServer( id = "login", data = user_base, user_col = user, pwd_col = password, log_out = reactive(logout_init()) ) # call the logout module with reactive trigger to hide/show logout_init <- shinyauthr::logoutServer( id = "logout", active = reactive(credentials()$user_auth) ) output$user_table <- renderTable({ # use req to only render results when credentials()$user_auth is TRUE req(credentials()$user_auth) credentials()$info }) } if (interactive()) shinyApp(ui = ui, server = server)
library(shiny) # dataframe that holds usernames, passwords and other user data user_base <- dplyr::tibble( user = c("user1", "user2"), password = c("pass1", "pass2"), permissions = c("admin", "standard"), name = c("User One", "User Two") ) ui <- fluidPage( # add logout button UI div(class = "pull-right", shinyauthr::logoutUI(id = "logout")), # add login panel UI function shinyauthr::loginUI(id = "login"), # setup table output to show user info after login tableOutput("user_table") ) server <- function(input, output, session) { # call login module supplying data frame, # user and password cols and reactive trigger credentials <- shinyauthr::loginServer( id = "login", data = user_base, user_col = user, pwd_col = password, log_out = reactive(logout_init()) ) # call the logout module with reactive trigger to hide/show logout_init <- shinyauthr::logoutServer( id = "logout", active = reactive(credentials()$user_auth) ) output$user_table <- renderTable({ # use req to only render results when credentials()$user_auth is TRUE req(credentials()$user_auth) credentials()$info }) } if (interactive()) shinyApp(ui = ui, server = server)
Shiny UI Module for use with loginServer
loginUI( id, title = "Please log in", user_title = "User Name", pass_title = "Password", login_title = "Log in", error_message = "Invalid username or password!", additional_ui = NULL, cookie_expiry = 7 )
loginUI( id, title = "Please log in", user_title = "User Name", pass_title = "Password", login_title = "Log in", error_message = "Invalid username or password!", additional_ui = NULL, cookie_expiry = 7 )
id |
An ID string that corresponds with the ID used to call the module's server function |
title |
header title for the login panel |
user_title |
label for the user name text input |
pass_title |
label for the password text input |
login_title |
label for the login button |
error_message |
message to display after failed login |
additional_ui |
additional shiny UI element(s) to add below login button. Wrap multiple inside |
cookie_expiry |
number of days to request browser to retain login cookie |
Shiny UI login panel with user name text input, password text input and login action button.
library(shiny) # dataframe that holds usernames, passwords and other user data user_base <- dplyr::tibble( user = c("user1", "user2"), password = c("pass1", "pass2"), permissions = c("admin", "standard"), name = c("User One", "User Two") ) ui <- fluidPage( # add logout button UI div(class = "pull-right", shinyauthr::logoutUI(id = "logout")), # add login panel UI function shinyauthr::loginUI(id = "login"), # setup table output to show user info after login tableOutput("user_table") ) server <- function(input, output, session) { # call login module supplying data frame, # user and password cols and reactive trigger credentials <- shinyauthr::loginServer( id = "login", data = user_base, user_col = user, pwd_col = password, log_out = reactive(logout_init()) ) # call the logout module with reactive trigger to hide/show logout_init <- shinyauthr::logoutServer( id = "logout", active = reactive(credentials()$user_auth) ) output$user_table <- renderTable({ # use req to only render results when credentials()$user_auth is TRUE req(credentials()$user_auth) credentials()$info }) } if (interactive()) shinyApp(ui = ui, server = server)
library(shiny) # dataframe that holds usernames, passwords and other user data user_base <- dplyr::tibble( user = c("user1", "user2"), password = c("pass1", "pass2"), permissions = c("admin", "standard"), name = c("User One", "User Two") ) ui <- fluidPage( # add logout button UI div(class = "pull-right", shinyauthr::logoutUI(id = "logout")), # add login panel UI function shinyauthr::loginUI(id = "login"), # setup table output to show user info after login tableOutput("user_table") ) server <- function(input, output, session) { # call login module supplying data frame, # user and password cols and reactive trigger credentials <- shinyauthr::loginServer( id = "login", data = user_base, user_col = user, pwd_col = password, log_out = reactive(logout_init()) ) # call the logout module with reactive trigger to hide/show logout_init <- shinyauthr::logoutServer( id = "logout", active = reactive(credentials()$user_auth) ) output$user_table <- renderTable({ # use req to only render results when credentials()$user_auth is TRUE req(credentials()$user_auth) credentials()$info }) } if (interactive()) shinyApp(ui = ui, server = server)
Deprecated. Use logoutServer instead.
input |
shiny input |
output |
shiny output |
session |
shiny session |
active |
[reactive] supply the returned |
Shiny authentication module for use with logoutUI
Call via shiny::callModule(shinyauthr::logout, "id", ...)
This function is now deprecated in favour of logoutServer which uses shiny's new moduleServer method as opposed to the callModule method used by this function. See the logoutServer documentation For details on how to migrate.
Reactive boolean, to be supplied as the log_out
argument of the
login module to trigger the logout process
## Not run: logout_init <- shiny::callModule( logout, id = "logout", active = reactive(user_credentials()$user_auth) ) ## End(Not run)
## Not run: logout_init <- shiny::callModule( logout, id = "logout", active = reactive(user_credentials()$user_auth) ) ## End(Not run)
Shiny authentication module for use with logoutUI
logoutServer(id, active, ...)
logoutServer(id, active, ...)
id |
An ID string that corresponds with the ID used to call the module's UI function |
active |
|
... |
arguments passed to toggle |
This module uses shiny's new moduleServer method as opposed to the callModule method used by the now deprecated login function and must be called differently in your app. For details on how to migrate see the 'Migrating from callModule to moduleServer' section of Modularizing Shiny app code.
Reactive boolean, to be supplied as the log_out
argument of the
loginServer module to trigger the logout process
library(shiny) # dataframe that holds usernames, passwords and other user data user_base <- dplyr::tibble( user = c("user1", "user2"), password = c("pass1", "pass2"), permissions = c("admin", "standard"), name = c("User One", "User Two") ) ui <- fluidPage( # add logout button UI div(class = "pull-right", shinyauthr::logoutUI(id = "logout")), # add login panel UI function shinyauthr::loginUI(id = "login"), # setup table output to show user info after login tableOutput("user_table") ) server <- function(input, output, session) { # call login module supplying data frame, # user and password cols and reactive trigger credentials <- shinyauthr::loginServer( id = "login", data = user_base, user_col = user, pwd_col = password, log_out = reactive(logout_init()) ) # call the logout module with reactive trigger to hide/show logout_init <- shinyauthr::logoutServer( id = "logout", active = reactive(credentials()$user_auth) ) output$user_table <- renderTable({ # use req to only render results when credentials()$user_auth is TRUE req(credentials()$user_auth) credentials()$info }) } if (interactive()) shinyApp(ui = ui, server = server)
library(shiny) # dataframe that holds usernames, passwords and other user data user_base <- dplyr::tibble( user = c("user1", "user2"), password = c("pass1", "pass2"), permissions = c("admin", "standard"), name = c("User One", "User Two") ) ui <- fluidPage( # add logout button UI div(class = "pull-right", shinyauthr::logoutUI(id = "logout")), # add login panel UI function shinyauthr::loginUI(id = "login"), # setup table output to show user info after login tableOutput("user_table") ) server <- function(input, output, session) { # call login module supplying data frame, # user and password cols and reactive trigger credentials <- shinyauthr::loginServer( id = "login", data = user_base, user_col = user, pwd_col = password, log_out = reactive(logout_init()) ) # call the logout module with reactive trigger to hide/show logout_init <- shinyauthr::logoutServer( id = "logout", active = reactive(credentials()$user_auth) ) output$user_table <- renderTable({ # use req to only render results when credentials()$user_auth is TRUE req(credentials()$user_auth) credentials()$info }) } if (interactive()) shinyApp(ui = ui, server = server)
Shiny UI Module for use with logoutServer
logoutUI( id, label = "Log out", icon = NULL, class = "btn-danger", style = "color: white;" )
logoutUI( id, label = "Log out", icon = NULL, class = "btn-danger", style = "color: white;" )
id |
An ID string that corresponds with the ID used to call the module's server function |
label |
label for the logout button |
icon |
An optional |
class |
bootstrap class for the logout button |
style |
css styling for the logout button |
Shiny UI action button
library(shiny) # dataframe that holds usernames, passwords and other user data user_base <- dplyr::tibble( user = c("user1", "user2"), password = c("pass1", "pass2"), permissions = c("admin", "standard"), name = c("User One", "User Two") ) ui <- fluidPage( # add logout button UI div(class = "pull-right", shinyauthr::logoutUI(id = "logout")), # add login panel UI function shinyauthr::loginUI(id = "login"), # setup table output to show user info after login tableOutput("user_table") ) server <- function(input, output, session) { # call login module supplying data frame, # user and password cols and reactive trigger credentials <- shinyauthr::loginServer( id = "login", data = user_base, user_col = user, pwd_col = password, log_out = reactive(logout_init()) ) # call the logout module with reactive trigger to hide/show logout_init <- shinyauthr::logoutServer( id = "logout", active = reactive(credentials()$user_auth) ) output$user_table <- renderTable({ # use req to only render results when credentials()$user_auth is TRUE req(credentials()$user_auth) credentials()$info }) } if (interactive()) shinyApp(ui = ui, server = server)
library(shiny) # dataframe that holds usernames, passwords and other user data user_base <- dplyr::tibble( user = c("user1", "user2"), password = c("pass1", "pass2"), permissions = c("admin", "standard"), name = c("User One", "User Two") ) ui <- fluidPage( # add logout button UI div(class = "pull-right", shinyauthr::logoutUI(id = "logout")), # add login panel UI function shinyauthr::loginUI(id = "login"), # setup table output to show user info after login tableOutput("user_table") ) server <- function(input, output, session) { # call login module supplying data frame, # user and password cols and reactive trigger credentials <- shinyauthr::loginServer( id = "login", data = user_base, user_col = user, pwd_col = password, log_out = reactive(logout_init()) ) # call the logout module with reactive trigger to hide/show logout_init <- shinyauthr::logoutServer( id = "logout", active = reactive(credentials()$user_auth) ) output$user_table <- renderTable({ # use req to only render results when credentials()$user_auth is TRUE req(credentials()$user_auth) credentials()$info }) } if (interactive()) shinyApp(ui = ui, server = server)
Launch an example shiny app using shinyauthr authentication modules. Use user1 pass1 or user2 pass2 to login.
runExample(example = c("basic", "shinydashboard", "navbarPage"))
runExample(example = c("basic", "shinydashboard", "navbarPage"))
example |
The app to launch. Options are "basic", "shinydashboard" or "navbarPage" |
No return value, a shiny app is launched.
## Only run this example in interactive R sessions if (interactive()) { runExample("basic") runExample("shinydashboard") runExample("navbarPage") }
## Only run this example in interactive R sessions if (interactive()) { runExample("basic") runExample("shinydashboard") runExample("navbarPage") }