{firebase}的作者在包示例/文档中显示了how to use the pre-built UI。
我想为我正在做的一个R Shiny项目构建我自己的登录/注册表单。如何将其链接到firebase,以便我不必使用预构建的登录表单?
我正在使用电子邮件认证方法。
这是我的登录/注册表:
x1c 0d1x的数据
global.R
library(shiny)
字符串
ui.R
ui <- bslib::page(
title = "Custom Sign In/Up Form",
theme = bslib::bs_theme(version = 5),
lang = "en",
tags$div(
class = "container",
tabsetPanel(
id = "auth_form",
type = "hidden",
tabPanelBody(
value = "signin",
tags$div(
class = "d-flex justify-content-center align-items-center vh-100",
tags$div(
mod_auth_form_ui(id = "signin"),
tags$p(
class = "mt-2",
"First time?",
actionLink(inputId = "go_to_signup", label = "Sign up here")
)
)
)
),
tabPanelBody(
value = "signup",
tags$div(
class = "d-flex justify-content-center align-items-center vh-100",
tags$div(
mod_auth_form_ui(id = "signup", type = "signup"),
tags$p(
class = "mt-2",
"Already have an account?",
actionLink(inputId = "go_to_signin", label = "Sign in here")
)
)
)
)
)
)
)
型
server.R
server <- \(input, output, session) {
observeEvent(input$go_to_signup, {
freezeReactiveValue(x = input, name = "auth_form")
updateTabsetPanel(
session = session,
inputId = "auth_form",
selected = "signup"
)
})
observeEvent(input$go_to_signin, {
freezeReactiveValue(x = input, name = "auth_form")
updateTabsetPanel(
session = session,
inputId = "auth_form",
selected = "signin"
)
})
}
型
R/mod_auth_form_ui.R
#' Auth form module UI
#'
#' @param id Module id
#' @param type Type of form to create. Either "signin" (default) or "signup".
#' @return [shiny::tags$form()]
mod_auth_form_ui <- \(id, type = "signin") {
ns <- NS(id)
email_input_tag_q <- textInput(
inputId = ns("email"),
label = "Email address",
placeholder = "[email protected]"
) |>
htmltools::tagQuery()
email_input_tag_q$find("input")$removeAttrs("type")$addAttrs("type" = "email")
email_input <- email_input_tag_q$allTags()
password_input <- passwordInput(inputId = ns("password"), label = "Password")
username_input <- textInput(
inputId = ns("username"),
label = "First & last name",
placeholder = "John Doe"
)
submit_btn <- actionButton(
inputId = ns("submit"),
label = "Submit",
class = "btn btn-primary",
type = "submit"
)
tags$form(
tags$div(class = "mb-3", email_input),
if (type == "signup") {
tags$div(class = "mb-3", username_input)
},
tags$div(class = "mb-3", password_input),
submit_btn
)
}
型
1条答案
按热度按时间i86rm4rw1#
在
Shiny
应用程序中使用firebase R packag
和Firebase JavaScript SDK
。firebase R package
提供了初始化、配置和使用firebase
进行身份验证的功能,而firebaseJavaScript
SDK允许您从Web浏览器与firebase auth服务进行交互。library(firebase)
和use_firebase()
将firebase R包和firebase JavaScript SDK加载到global.R
文件中firebase_init()
初始化server.R
文件中的firebase应用程序,并将路径作为参数传递给JSON配置文件。observeEvent()
和input$submit
将事件侦听器添加到server.R
文件中的登录/使用表单。firebase_auth_email()
函数登录用户或使用用户的电子邮件地址和密码注册用户,并将input$email
和input$password
作为参数传递。firebase_user()
函数获取当前用户的信息,如email、uid、显示名等。firebase_sign_out()
函数注销当前用户。参考文献: