Shiny authentication module to be used with signupUI. It uses shiny's new moduleServer method.
signupServer(id, credentials, mongodb = NULL, email = NULL)
The ID for the Server module.
reactive supply the returned reactive from signinServer
here to pass users and btn_signup
and btn_forgotpw
infos.
A mongodb connection object from mongolite::mongo.
Set to NULL
: use local storage.
A email template. Set to NULL
: skip code verify step.
See email_template and create_email.
a data.frame with six columns, containing newly signed-up user.
library(shiny)
library(mongolite)
library(shinyAuthX)
## default mongodb database server for testing: works only with `mtcars`
con <- mongo("mtcars", url = "mongodb+srv://readwrite:test@cluster0-84vdt.mongodb.net/test")
## remove any existing rows
con$drop()
## check
con$count()
#> [1] 0
# add users_base to con
create_dummy_users() |>
con$insert()
#> List of 5
#> $ nInserted : num 3
#> $ nMatched : num 0
#> $ nRemoved : num 0
#> $ nUpserted : num 0
#> $ writeErrors: list()
con$count()
#> [1] 3
ui <- fluidPage(
# add signout button UI
div(class = "pull-right", signoutUI(id = "signout")),
# add signin panel UI function with signup panel
signinUI(id = "signin", .add_forgotpw = FALSE, .add_btn_signup = TRUE),
# add signup panel
signupUI("signup"),
# 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 = con$find('{}'), ## add mongodb connection instead of tibble
sodium_hashed = TRUE,
reload_on_signout = FALSE,
signout = reactive(signout_init())
)
# call signup module supplying credentials() reactive
signupServer(
id = "signup", credentials = credentials, mongodb = con
)
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)