使用{firebase}为R Shiny定制的登录/注册表单

vsikbqxv  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(49)

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

i86rm4rw

i86rm4rw1#

Shiny应用程序中使用firebase R packagFirebase JavaScript SDKfirebase R package提供了初始化、配置和使用firebase进行身份验证的功能,而firebase JavaScript 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$emailinput$password作为参数传递。
  • 使用firebase_user()函数获取当前用户的信息,如email、uid、显示名等。
  • 使用firebase_sign_out()函数注销当前用户。

参考文献:

  1. Calling Firebase auth UI from an R Shiny app - Stack Overflow的一个。
  2. Adding Firebase Authentication to Shiny | R-bloggers的一个。
  3. Authenticate R Shiny Application with Firebase的函数。
  4. Firebase App Script的函数。
  5. Firebase Auth Script的一个。
  6. Firebase Console的一个。

相关问题