Package 'login'

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

Help Index


Returns a function to send emails using the emayili package.

Description

This returns a function that can be used with the login_server(). Specifically, the function takes two parameters, to_email and message.

Usage

emayili_emailer(
  email_host = NULL,
  email_port = NULL,
  email_username = NULL,
  email_password = NULL,
  from_email = NULL
)

Arguments

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 login_server().

Value

returns a function to send an email using the emayili package.

See Also

login_server()


Display Shiny elements only if the user is logged in.

Description

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.

Usage

is_logged_in(id, ...)

Arguments

id

id unique ID for the Shiny Login module.

...

Shiny UI elements.

Value

a shiny::conditionalPanel() object.

See Also

login_server()


Display Shiny elements only if the user is not logged in.

Description

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.

Usage

is_not_logged_in(id, ...)

Arguments

id

id unique ID for the Shiny Login module.

...

Shiny UI elements.

Value

a shiny::conditionalPanel()

See Also

login_server()


Login server module.

Description

This is the main server logic for the login Shiny module to be included in server.R side,.

Usage

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"
)

Arguments

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: to_email for the address to send the email, subject for the subject of the email and message for the contents of the email address. See emayili_emailer() for an example.

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 NULL to disable cookies.

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 ⁠\%s⁠ somewhere in the message to include the code.

reset_email_message

Email message sent to reset password. Include ⁠\%s⁠ somewhere in the message to include the code.

enclosing_panel

the Shiny element that contains all the UI elements. The default is shiny::wellPanel(). If you wish a more subtle appearance htmltools::div() is a reasonable choice.

code_length

the number of digits of codes emailed for creating accounts (if verify_email == TRUE) or resetting passwords.

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 digest::digest() for more details.

shinybusy_spin

Style of the spinner when sending emails. See shinybusy::use_busy_spinner() for more information.

shinybusy_position

Position of the spinner when sending emails. See shinybusy::use_busy_spinner() for more information.

Value

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.

Examples

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)
}

Login UI elements.

Description

This will render (if the user is not logged in) text boxes and buttons for the user to login.

Usage

login_ui(id)

Arguments

id

id unique ID for the Shiny Login module.

Value

a shiny::div() object.


Logout button.

Description

Render a button for the user to logout.

Usage

logout_button(
  id,
  label = "Logout",
  icon = shiny::icon("right-from-bracket"),
  style = "",
  check_login = TRUE
)

Arguments

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 is_logged_in().

Value

a shiny::actionButton() if the user is logged in.


UI for creating a new user account.

Description

This will render the UI for users to create an account.

Usage

new_user_ui(id)

Arguments

id

id unique ID for the Shiny Login module.

Value

shiny object containing the input fields for a user to create an account.


Password input textbox.

Description

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.

Usage

passwdInput(inputId, label, value)

Arguments

inputId

ID for the input.

label

label for the textbox.

value

default value.

Value

a shiny::tagList() object.


UI for resetting password.

Description

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.

Usage

reset_password_ui(id)

Arguments

id

id unique ID for the Shiny Login module.

Value

a shiny object containing the input fields for a user to reset their password.


Button to show the parameter modal dialog box.

Description

Button to show the parameter modal dialog box.

Action button that will clear the parameters and remove cookies.

Usage

showParamButton(id, label = "Edit Parameters", icon = shiny::icon("gear"))

clearParamButton(id, label = "Clear Parameters", icon = shiny::icon("eraser"))

Arguments

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 shiny::icon() to appear on the button.


A simple validation check to ensure the parameters are not blank.

Description

A simple validation check to ensure the parameters are not blank.

Usage

simple_parameter_validator(values, types)

Arguments

values

a named list of values to check.

types

a character vector of the input types.

Value

either TRUE if the validation passes or a character string indicating why the validation failed.


JavaScript and CSS dependencies.

Description

This ensures the JavaScript and CSS dependencies are available to the client. Files are located in ⁠assets/⁠ folder when installed..

Usage

use_login()

Value

a htmltools::htmlDependency() object defining the JavaScript and CSS files.


Server module for user parameter input

Description

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.

Usage

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()
)

Arguments

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 character, numeric, date, file, logical, password, or select. Note that for select you must specify the choices parameter in the input_params. For example, input_params <- list('param_name' = c('Option A', 'Option B'))

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. shiny::div).

modal_size

One of "s" for small, "m" (the default) for medium, "l" for large, or "xl" for extra large. See shiny::modalDialog() for more info.

save_label

label for the save button in the modal dialog.

cancel_label

label for the cancel button in the modal dialog, or NULL to exclude. If available a user clicking the cancel button will bypass any validation checks.

allow_cookies

if TRUE the user can opt to save the values as cookies. The user will still be presented with a checkbox to not save parameters as cookies.

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 simple_parameter_validator() for an example. The function should take two parameters: a named list of values to check and a vector of value types (see param_types). The function should return either TRUE if the validation passes or a character string indicating why the validation failed. Set to NULL to disable validation.

open_on_startup

if TRUE the modal dialog will be shown if the parameters could not be read from cookies (i.e. they have not been set yet).

cookie_password

key used to encrypt/decrypt cookies. See encrypt_cookie() and decrypt_cookie().

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 params.

Details

See the parameters vignette for more information.

Value

a shiny::reactiveValues object.