-
Notifications
You must be signed in to change notification settings - Fork 81
/
logout.R
106 lines (100 loc) · 3.62 KB
/
logout.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#' logout UI module
#'
#' Shiny UI Module for use with \link{logoutServer}
#'
#' @param id An ID string that corresponds with the ID used to call the module's server function
#' @param label label for the logout button
#' @param icon An optional \code{\link[shiny]{icon}} to appear on the button.
#' @param class bootstrap class for the logout button
#' @param style css styling for the logout button
#'
#' @return Shiny UI action button
#' @example inst/shiny-examples/basic/app.R
#' @export
logoutUI <- function(id, label = "Log out", icon = NULL, class = "btn-danger", style = "color: white;") {
ns <- shiny::NS(id)
shinyjs::hidden(
shiny::actionButton(ns("button"), label, icon = icon, class = class, style = style)
)
}
#' logout server module
#'
#' Shiny authentication module for use with \link{logoutUI}
#'
#' This module uses shiny's new \link[shiny]{moduleServer} method as opposed to the \link[shiny]{callModule}
#' method used by the now deprecated \link{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
#' \href{https://shiny.rstudio.com/articles/modules.html}{Modularizing Shiny app code}.
#'
#' @param id An ID string that corresponds with the ID used to call the module's UI function
#' @param active \code{reactive} supply the returned \code{user_auth} boolean reactive from \link{loginServer}
#' here to hide/show the logout button
#' @param ... arguments passed to \link[shinyjs]{toggle}
#'
#' @return Reactive boolean, to be supplied as the \code{log_out} argument of the
#' \link{loginServer} module to trigger the logout process
#'
#' @example inst/shiny-examples/basic/app.R
#' @export
logoutServer <- function(id, active, ...) {
shiny::moduleServer(
id,
function (input, output, session) {
shiny::observe({
shinyjs::toggle(id = "button", condition = active(), ...)
})
# return reactive logout button tracker
shiny::reactive({
input$button
})
}
)
}
#' logout server module (deprecated)
#'
#' Deprecated. Use \link{logoutServer} instead.
#'
#' Shiny authentication module for use with \link{logoutUI}
#'
#' Call via \code{shiny::callModule(shinyauthr::logout, "id", ...)}
#'
#' This function is now deprecated in favour of \link{logoutServer} which uses shiny's new \link[shiny]{moduleServer}
#' method as opposed to the \link[shiny]{callModule} method used by this function.
#' See the \link{logoutServer} documentation For details on how to migrate.
#'
#' @usage NULL
#'
#' @param input shiny input
#' @param output shiny output
#' @param session shiny session
#' @param active [reactive] supply the returned \code{user_auth} boolean reactive from \link{login}
#' here to hide/show the logout button
#'
#' @return Reactive boolean, to be supplied as the \code{log_out} argument of the
#' \link{login} module to trigger the logout process
#'
#' @examples
#' \dontrun{
#' logout_init <- shiny::callModule(
#' logout,
#' id = "logout",
#' active = reactive(user_credentials()$user_auth)
#' )
#' }
#'
#' @export
logout <- function(input, output, session, active) {
.Deprecated(msg = paste0("'shinyauthr::logout' is deprecated. Use 'shinyauthr::logoutServer' instead.\n",
"See ?shinyauthr::logoutServer for information on how to switch."))
shiny::observe({
if (active()) {
shinyjs::show(id = "button", anim = TRUE, time = 1, animType = "fade")
} else {
shinyjs::hide(id = "button", anim = TRUE, time = 1, animType = "fade")
}
})
# return reactive logout button tracker
shiny::reactive({
input$button
})
}