login_server: Login server module

View source: R/deprecated_login_server.R

login_serverR Documentation

Login server module

Description

[Deprecated]

This function has become deprecated. New RegLog system is based on RegLogServer R6 class.

Shiny server module for the optional login/registration system This function creates a server module to handle other modules of the system: login_UI(), password_reset_UI() and register_UI It uses database contained in 'googlesheet' file on your 'gdrive' or 'SQLite' database locally to read and write data of the users. You need to create a 'googlesheet' or 'SQLite' using create_gsheet_db() or create_sqlite_db() respectively.

Usage

login_server(
  id = "login_system",
  db_method,
  mail_method,
  appname,
  appaddress,
  lang = "en",
  gsheet_file,
  sqlite_db,
  gmailr_user,
  emayili_user,
  emayili_password,
  emayili_host,
  emayili_port,
  custom_txts = NULL,
  use_login_modals = list(UserNotFound = T, WrongPass = T, Success = T)
)

Arguments

id

the id of the module. Defaults to "login_system" for all of the modules contained within the package. If you plan to use serveral login systems inside your app or for any other reason need to change it, remember to keep consistent id for all elements of module.

db_method

the character string containing chosen database container, either: "gsheet" (needing installation of 'googlesheets4' package) or "sqlite" (needing installation of 'DBI' and 'RSQLite' packages)

mail_method

the character string containing chosen method of sending emails, either: "gmailr" (needing installation of 'gmailr' package) "emayili" (needing installation of 'emayili' package)

appname

the character string containing the name of your application (used in automatic e-mails for information purposes)

appaddress

the character value containing the web address of your application (used in automatic e-mails for information purposes)

lang

specifies the app used language. Accepts "en" or "pl". Defaults to "en"

gsheet_file

the ID of your 'googlesheet' file holding the database. It is contained within URL address of your googlesheet (for: db_method = "gsheet")

sqlite_db

the path to your 'SQLite' database (for: db_method = "sqlite")

gmailr_user

your gmail address (for: db_method = "gmailr")

emayili_user

your email address, also used as login to your email account (for: db_method = "emayili")

emayili_password

password to your email account (for: db_method = "emayili")

emayili_host

host of your email box (for: db_method = "emayili")

emayili_port

port of your email box (for: db_method = "emayili")

custom_txts

named list containing customized texts. For more details, see documentation for 'reglog_txt'. Provided list can contain only elements used by this function, but it is recommended to provide the same list for every 'shiny.reglog' function

use_login_modals

list of logicals indicating if the modalDialog after log-in should be shown. Defaults to named list of logicals:

  • UserNotFound = T

  • WrongPass = T

  • Success = T

Details

The module logic creates a reactiveValues() object with loaded database of users and reset codes stored in session$userData. It allows to cut the reading from database to only one read per loading of the app - unfortunately it makes the app run slowly if the database of users gets very long.

Registration of new account mails the confirmation e-mail to the end user on provided e-mail.

Provided e-mail is needed for password reset: 10 digits code is generated and mailed to the user to confirm its identity. Reset code remains valid for 24 hours.

Authorization

  • When using db_method of "gsheet" you need to authorize access to your google drive outside of the functions (using googlesheets4:gs_auth() with default scopes: "https://www.googleapis.com/auth/spreadsheets")

  • When using mail_method of "emayili" you need to allow "less secure apps" to use your mailbox

  • When using mail_method of "gmailr" you need to authorize access to your gmail box by creating Oauth2 App on 'Google Cloud Platform' and passing it to gmailr::gm_auth_configure() and allowing scopes: "https://www.googleapis.com/auth/gmail.send"

Security

  • Both passwords and reset codes are hashed with the help of 'scrypt' package for the extra security

  • gmailr mail_method seems to be more secure if you intend to use 'gmail' account to send emails. 'emayili' is suggested only when using other mailboxes.

Value

reactiveValues() object with three elements:

is_logged, containing boolean describing authorization status

user_id, containing the logged user identification name. When not logged, it contains the timestamp of session start

user_mail, containing the logged user mail. When not logged, it is empty character string of nchar() value 0: ("")

last_state_change, containing string describing last change of login system state. Currently only supports state changes during login procedure

See Also

login_UI() for the login window in UI

password_reset_UI() for the password reset window in UI

register_UI() for the registration window in UI

Examples

## Only run this example in interactive R sessions

if(interactive()){
  
  #### example of db_method = "sqlite" and mail_method = "emayili"
  
  library(shiny)
  library(shiny.reglog)
  
  # initializing sqlite
  
  sqlite.path <- tempfile(fileext = "sqlite")
  create_sqlite_db(sqlite.path)
  database <- sqlite_get_db(sqlite.path)
  
  # Define UI containing shiny.reglog modules
  ui <- fluidPage(
    
    headerPanel(title = "shiny.reglog test"),
    
    tabsetPanel(
      tabPanel("Values", 
               # table of returned data for active user
               dataTableOutput("active_user_values"),
               # table of session$userData$reactive_db$user_db loaded at the start of session
               dataTableOutput("user_db"),
               # table of session$userData$reactive_db$reset_db loaded at the start of session
               dataTableOutput("reset_db")
      ),
      tabPanel("Login", login_UI()),
      tabPanel("Register", register_UI()),
      tabPanel("Reset Password", password_reset_UI()),
      tabPanel("Logout", logout_button())
      
    )
  )
  
  server <- function(input, output, session) {
    
    # login server with specified methods for database and mailing
    # to run it you need to replace placeholders with your details and 
    # cofigure it for your needs
    
    auth <- login_server(
      db_method = "sqlite",
      mail_method = "emayili",
      appname = "shiny.reglog example",
      appaddress = "not-on-net.com",
      sqlite_db = sqlite.path,
      # arguments below need configuration for your mailing account
      emayili_user = "your_email_address",
      emayili_password = "your_email_password",
      emayili_port = "your_email_box_port",
      emayili_host = "your_email_box_host"
    )
    
    # table of values returned by login_server
    
    output$active_user_values <- renderDataTable({
      data.frame(is_logged = auth$is_logged,
                 user_id = auth$user_id,
                 user_mail = auth$user_mail
      )
    })
    
    # tibbles contained within session$userData$reactive_db
   
    output$user_db <- renderDataTable(
      session$userData$reactive_db$user_db
    )
    
    output$reset_db <- renderDataTable(
      session$userData$reactive_db$reset_db
    )
  }
  
  # Run the application 
  shinyApp(ui = ui, server = server)
  
}


shiny.reglog documentation built on Aug. 31, 2022, 1:06 a.m.