| Title: | 'shiny' Login Module |
|---|---|
| Description: | Framework for adding authentication to 'shiny' applications. Provides flexibility as compared to other options for where user credentials are saved, allows users to create their own accounts, and password reset functionality. Bryer (2024) <doi:10.5281/zenodo.10987876>. |
| Authors: | Jason Bryer [aut, cre] (ORCID: <https://orcid.org/0000-0002-2454-0402>) |
| Maintainer: | Jason Bryer <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.0.0 |
| Built: | 2026-05-12 08:23:45 UTC |
| Source: | https://github.com/jbryer/login |
emayili package.This returns a function that can be used with the login_server(). Specifically,
the function takes two parameters, to_email and message.
emayili_emailer( email_host = NULL, email_port = NULL, email_username = NULL, email_password = NULL, from_email = NULL )emayili_emailer( email_host = NULL, email_port = NULL, email_username = NULL, email_password = NULL, from_email = NULL )
email_host |
SMTP email host. |
email_port |
SMPT email port. |
email_username |
username for the SMTP server. |
email_password |
password for the SMTP server. |
from_email |
the from email address sent from |
returns a function to send an email using the emayili package.
Encrypt cookie.
Decrypt cookie.
encrypt_cookie(cookie_key, message) decrypt_cookie(cookie_key, message)encrypt_cookie(cookie_key, message) decrypt_cookie(cookie_key, message)
cookie_key |
encryption key. |
message |
string to decrypt. |
This function can be used on the Shiny UI side. It will check to see if the user is logged in, if so the other Shiny elements will be displayed.
is_logged_in(id, ...)is_logged_in(id, ...)
id |
id unique ID for the Shiny Login module. |
... |
Shiny UI elements. |
a shiny::conditionalPanel() object.
This function can be used on the Shiny UI side. It will. check to see if the user is not logged in, if so the other Shiny elements will be displayed.
is_not_logged_in(id, ...)is_not_logged_in(id, ...)
id |
id unique ID for the Shiny Login module. |
... |
Shiny UI elements. |
This is the main server logic for the login Shiny module to be included
in server.R side,.
login_server( id, db_conn = NULL, users_table = "users", activity_table = "users_activity", emailer = NULL, new_account_subject = "Verify your new account", reset_password_subject = "Reset password", verify_email = !is.null(emailer), additional_fields = NULL, cookie_name = "loginusername", cookie_expiration = 30, cookie_password = NULL, username_label = "Email:", password_label = "Password:", create_account_label = "Create Account", create_account_message = NULL, reset_email_message = NULL, enclosing_panel = shiny::wellPanel, code_length = 6, salt = NULL, salt_algo = "sha512", shinybusy_spin = "fading-circle", shinybusy_position = "full-page" )login_server( id, db_conn = NULL, users_table = "users", activity_table = "users_activity", emailer = NULL, new_account_subject = "Verify your new account", reset_password_subject = "Reset password", verify_email = !is.null(emailer), additional_fields = NULL, cookie_name = "loginusername", cookie_expiration = 30, cookie_password = NULL, username_label = "Email:", password_label = "Password:", create_account_label = "Create Account", create_account_message = NULL, reset_email_message = NULL, enclosing_panel = shiny::wellPanel, code_length = 6, salt = NULL, salt_algo = "sha512", shinybusy_spin = "fading-circle", shinybusy_position = "full-page" )
id |
unique ID for the Shiny Login module. |
db_conn |
a DBI database connection. |
users_table |
the name of the table in the database to store credentials. |
activity_table |
the name of the table in the database to log login and logout activity. |
emailer |
function used to send email messages. The function should have
have three parameters: |
new_account_subject |
the subject used for verifying new accounts. |
reset_password_subject |
the subject of password reset emails. |
verify_email |
if true new accounts will need to verify their email address before the account is crated. This is done by sending a six digit code to the email address. |
additional_fields |
a character vector of additional fields the user is asked to fill in at the when creating a new account. The names of the vector correspond to the variable names and the values will be used as the input labels. |
cookie_name |
the name of the cookie saved. Set to |
cookie_expiration |
the number of days after which the cookie will expire. |
cookie_password |
password used to encrypt cookies saved in the browser. |
username_label |
label used for text inputs of username. |
password_label |
label used for text inputs of password. |
create_account_label |
label for the create account button. |
create_account_message |
Email message sent to confirm email when creating
a new account. Include |
reset_email_message |
Email message sent to reset password. Include |
enclosing_panel |
the Shiny element that contains all the UI elements.
The default is |
code_length |
the number of digits of codes emailed for creating accounts
(if |
salt |
a salt to use to encrypt the password before storing it in the database. |
salt_algo |
the algorithm used to encrypt the password. See
|
shinybusy_spin |
Style of the spinner when sending emails.
See |
shinybusy_position |
Position of the spinner when sending emails.
See |
a shiny::reactiveValues() object that includes two values: logged_in
(this is TRUE if the user is logged in) and username which has the
user's login username if logged in.
library(shiny) library(login) ###### User Interface ########################################################## ui <- fluidPage( titlePanel("Shiny Login Simple Demo"), p("You can login with 'test/test'."), login::login_ui(id = 'login_demo'), login::logout_button('login_demo'), hr(), div('Are you logged in? ', textOutput('is_logged_in')), div('Username: ', textOutput('username')), login::is_logged_in( id = 'login_demo', div("This only shows when you are logged in!") ), login::is_not_logged_in( id = 'login_demo', div("This only shows when you are NOT logged in!") ) ) ##### Server ################################################################### server <- function(input, output, session) { USER <- login::login_server( id = 'login_demo', db_conn = RSQLite::dbConnect(RSQLite::SQLite(), 'users.sqlite') ) observeEvent(USER$logged_in, { if(USER$logged_in) { shinyjs::hide(id = 'login_box') } else { shinyjs::show(id = "login_box") } }) output$is_logged_in <- renderText({ USER$logged_in }) output$username <- renderText({ USER$username }) } ##### Run the application ###################################################### if(interactive()) { shinyApp(ui = ui, server = server) }library(shiny) library(login) ###### User Interface ########################################################## ui <- fluidPage( titlePanel("Shiny Login Simple Demo"), p("You can login with 'test/test'."), login::login_ui(id = 'login_demo'), login::logout_button('login_demo'), hr(), div('Are you logged in? ', textOutput('is_logged_in')), div('Username: ', textOutput('username')), login::is_logged_in( id = 'login_demo', div("This only shows when you are logged in!") ), login::is_not_logged_in( id = 'login_demo', div("This only shows when you are NOT logged in!") ) ) ##### Server ################################################################### server <- function(input, output, session) { USER <- login::login_server( id = 'login_demo', db_conn = RSQLite::dbConnect(RSQLite::SQLite(), 'users.sqlite') ) observeEvent(USER$logged_in, { if(USER$logged_in) { shinyjs::hide(id = 'login_box') } else { shinyjs::show(id = "login_box") } }) output$is_logged_in <- renderText({ USER$logged_in }) output$username <- renderText({ USER$username }) } ##### Run the application ###################################################### if(interactive()) { shinyApp(ui = ui, server = server) }
This will render (if the user is not logged in) text boxes and buttons for the user to login.
login_ui(id)login_ui(id)
id |
id unique ID for the Shiny Login module. |
a shiny::div() object.
Render a button for the user to logout.
logout_button( id, label = "Logout", icon = shiny::icon("right-from-bracket"), style = "", check_login = TRUE )logout_button( id, label = "Logout", icon = shiny::icon("right-from-bracket"), style = "", check_login = TRUE )
id |
id unique ID for the Shiny Login module. |
label |
label of the logout button. |
icon |
icon for the logout button. |
style |
CSS styles for the logout button. |
check_login |
if TRUE this will call |
a shiny::actionButton() if the user is logged in.
This will render the UI for users to create an account.
new_user_ui(id)new_user_ui(id)
id |
id unique ID for the Shiny Login module. |
shiny object containing the input fields for a user to create an account.
This is an extension to Shiny's built in passwordInput by encrpting the password client side before sending it to the server. Although it is encrypted in the client using JavaScript it highly recommend that you also use an SSL certificate (for https) as well.
passwdInput(inputId, label, value)passwdInput(inputId, label, value)
inputId |
ID for the input. |
label |
label for the textbox. |
value |
default value. |
a shiny::tagList() object.
Displays UI for users to reset their password. In order for the password
reset feature to work credentials to a SMTP server must be passed to the
login_server() function.
reset_password_ui(id)reset_password_ui(id)
id |
id unique ID for the Shiny Login module. |
a shiny object containing the input fields for a user to reset their password.
Button to show the parameter modal dialog box.
Action button that will clear the parameters and remove cookies.
showParamButton(id, label = "Edit Parameters", icon = shiny::icon("gear")) clearParamButton(id, label = "Clear Parameters", icon = shiny::icon("eraser"))showParamButton(id, label = "Edit Parameters", icon = shiny::icon("gear")) clearParamButton(id, label = "Clear Parameters", icon = shiny::icon("eraser"))
id |
unique ID for the Shiny Login module. |
label |
The contents of the button or link–usually a text label, but you could also use any other HTML, like an image. |
icon |
An optional |
A simple validation check to ensure the parameters are not blank.
simple_parameter_validator(values, types)simple_parameter_validator(values, types)
values |
a named list of values to check. |
types |
a character vector of the input types. |
either TRUE if the validation passes or a character string indicating why the
validation failed.
This ensures the JavaScript and CSS dependencies are available to the
client. Files are located in assets/ folder when installed..
use_login()use_login()
a htmltools::htmlDependency() object defining the JavaScript and CSS files.
The primary purpose of this Shiny module is to provide a framework to get parameters from a user required for the Shiny application to run.
userParamServer( id, params, param_labels = params, param_types = rep("character", length(params)), param_defaults = rep("", length(params)), modal_title = "Application Settings", intro_message = "", modal_size = "l", save_label = "Save", cancel_label = NULL, allow_cookies = TRUE, save_cookie_label = "Save parameters as cookies.", validator = simple_parameter_validator, open_on_startup = TRUE, cookie_password = NULL, cookie_expiration = 30, input_params = list() )userParamServer( id, params, param_labels = params, param_types = rep("character", length(params)), param_defaults = rep("", length(params)), modal_title = "Application Settings", intro_message = "", modal_size = "l", save_label = "Save", cancel_label = NULL, allow_cookies = TRUE, save_cookie_label = "Save parameters as cookies.", validator = simple_parameter_validator, open_on_startup = TRUE, cookie_password = NULL, cookie_expiration = 30, input_params = list() )
id |
unique ID for the Shiny Login module. |
params |
a character vector with the name of the parameters. This should be valid R names (i.e. start with a letter, do not contain any spaces, etc.). |
param_labels |
labels used for the user inputs. |
param_types |
the type of value. Valid types include |
param_defaults |
default values for the params. |
modal_title |
title for the modal dialog. |
intro_message |
A message that is displayed at the top of the modal dialog. This can
be a character string or any valid Shiny container (e.g. |
modal_size |
One of "s" for small, "m" (the default) for medium, "l" for large, or "xl" for
extra large. See |
save_label |
label for the save button in the modal dialog. |
cancel_label |
label for the cancel button in the modal dialog, or |
allow_cookies |
if |
save_cookie_label |
label for the check box where the user can opt to save the parameter values as cookies. |
validator |
a function to validate the user inputs. See |
open_on_startup |
if |
cookie_password |
key used to encrypt/decrypt cookies. See |
cookie_expiration |
the number of days after which the cookie will expire. |
input_params |
additional parameters passed to the Shiny input. This should be a named list
where names correspond to |
See the parameters vignette for more information.
a shiny::reactiveValues object.