You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

shiny::showModal与shinymanager配合使用时认证失效问题

问题修复方案

核心原因

你的模态框在shinymanager身份验证流程完成前就触发加载,遮罩层拦截了登录页所有交互操作,导致登录输入框、提交按钮无法正常响应。你此前尝试使用req(input$intro)没有生效,是因为登录阶段模态框弹出时,input$intro还未被注册到Shiny输入对象中,校验逻辑无法触发。

修复代码

修改服务端逻辑,将模态框的触发时机调整为身份验证成功之后,完整可运行代码如下:

library(shiny)
library(shinydashboard)
library(shinymanager)

# Credentials as data.frame
credentials <- data.frame(
  user = c("shiny", "shinymanager"), # mandatory
  password = c("azerty", "12345"), # mandatory
  stringsAsFactors = FALSE
)

# Change labels
set_labels(
  language = "en",
  "Please authenticate" = "Bitte authentifizieren",
  "Username:" = "Benutzername:",
  "Password:" = "Passwort:",
  "Login" = "Anmeldung"
)

# Classic shinydashboard
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(),
  title = "Dashboard example"
)


# Wrap your UI with secure_app
ui <- secure_app(
  ui = ui,
  tags_top = 
    tags$div(
      tags$h4("Demo", style = "align:center"),
      tags$img(
        src = "https://www.r-project.org/logo/Rlogo.png", width = 100
      )
    ),
  # add information on bottom ?
  tags_bottom = tags$div(
    tags$p(
      "For any question, please  contact ",
      tags$a(
        href = "mailto:someone@example.com?Subject=Shiny%20aManager",
        target="_top", "administrator"
      )
    )
  )
)

# Server
server <- function(input, output, session) {
  
  # 先执行身份验证逻辑
  res_auth <- secure_server(
    check_credentials = check_credentials(credentials)
  )
  
  # 仅在身份验证成功后触发模态框
  observeEvent(res_auth$user, {
    req(res_auth$user) # 二次校验确保已完成身份验证
    showModal(modalDialog(
      includeHTML("intro_text.html"),
      easyClose = TRUE,
      footer = tagList(
        actionButton(inputId = "intro", label = "OK", icon = icon("info-circle"))
      )
    ))
  }, once = TRUE) # 可选添加,确保整个会话仅弹出一次模态框
  
  observeEvent(input$intro,{
    removeModal()
  })
  
}

shinyApp(ui, server)

修改说明

  • 调整代码顺序,优先初始化shinymanager的身份验证服务
  • 将模态框的触发条件从空值改为监听res_auth$user,该值仅在用户登录成功后才会返回有效内容
  • 新增req(res_auth$user)做双重校验,避免验证未完成时误触发
  • 可选添加once = TRUE参数,避免后续会话操作中重复弹出引导模态框

内容的提问来源于stack exchange,提问作者neves

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 15:00:00