Shiny app only logout when the logout button is pressed twice

I'm develping a Shiny app with a login system that uses a modal to collect username/password to login. The problem is: when the user is loged, and click to logout, first the modal opens and closes quickly and when the user clicks again then finnaly logout hapens. I already spended a lot of time trying to solve this and still wtihout a clue in how to solve this.

library(shinydashboard)
library(shiny)
library(sodium)


credentials <- data.frame(record_id = "1", username_id = "USER", tipodeusuario = "Usuário administrador", passod = password_store("password"), logins_complete = "2")

header <- dashboardHeader(
  title = "", 
  titleWidth = 0,
  tags$li(class = "dropdown",
          tags$li(class = "dropdown", textOutput("logged_user"), style = "padding-top: 1px; padding-bottom: 1px; color: #fff;"),
          tags$li(class = "dropdown", actionLink("openModal", textOutput("logintext")))))


sidebar <- dashboardSidebar(uiOutput("sidebarpanel"))

body <- dashboardBody(shinyjs::useShinyjs(), uiOutput("body"),
                      tags$head(tags$style(HTML(
                        '.myClass { 
        font-size: 20px;
        line-height: 50px;
        text-align: left;
        font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
        padding: 0 15px;
        overflow: hidden;
        color: white;
      }
    '))))

ui <- dashboardPage(header, sidebar, body, skin = "purple")

server <- function(input, output, session){
  
  reactive_credentials <- reactiveValues(credentials = credentials)
  
  logged_in <- reactiveValues(login = FALSE)
  
  observeEvent(input$openModal,{
    if(logged_in$login){
      removeModal()
      logged_in$login <- FALSE
    }
  })
  
  output$logintext <- renderText({
    if(logged_in$login) return("Logout")
    return("Login")
  })
  
  observe( {
    if (!(logged_in$login)) {
      if (!is.null(input$login)) {
        if (input$login > 0) {
          Username <- toupper(isolate(input$userName))
          Password <- isolate(input$passwd)
          if(length(which(reactive_credentials$credentials$username_id == Username))==1) { 
            pasmatch  <- reactive_credentials$credentials["passod"][which(reactive_credentials$credentials$username_id == Username),]
            pasverify <- password_verify(pasmatch, Password)
            if(pasverify) {
              logged_in$login <- TRUE
              removeModal()
            } else {
              shinyjs::toggle(id = "nomatch", anim = TRUE, time = 1, animType = "fade")
              shinyjs::delay(3000, shinyjs::toggle(id = "nomatch", anim = TRUE, time = 1, animType = "fade"))
            }
          } else {
            shinyjs::toggle(id = "nomatch", anim = TRUE, time = 1, animType = "fade")
            shinyjs::delay(3000, shinyjs::toggle(id = "nomatch", anim = TRUE, time = 1, animType = "fade"))
          }
        } 
      }
    }
  })
  
  
  observeEvent(input$openModal, {
    if(!(logged_in$login)){
      showModal(
        modalDialog(title = "LOGIN",
                    textInput("userName", placeholder="Usuário", label = tagList(icon("user"), "Usuário")),
                    passwordInput("passwd", placeholder="Senha", label = tagList(icon("unlock-alt"), "Senha")),
                    
                    div(
                      style = "text-align: center; width: 500px; max-width: 100%; margin: 0 auto; padding: 20px;",
                      actionButton("login", "ENTRAR", style = "text-align: center; color: white; background-color:#7f609b;
                                 padding: 10px 15px; width: 150px; cursor: pointer;
                                 font-size: 18px; font-weight: 600;"),
                      shinyjs::hidden(
                        div(id = "nomatch",
                            tags$p("Usuário ou senha incorretos",
                                   style = "color: red; font-weight: 600; 
                                            padding-top: 5px;font-size:16px;", 
                                   class = "text-center"))),
                      br(),
                      br(),
                      br(),
                    )
        )
        
      )
    }
  })
  
}

shinyApp(ui, server)

Hey jbsilva,
Do we really need all those libraries to reproduce the problem? Can you please condense the code a little?
Cheers

I edited the code removing all the unecessaries libraries.

library(shinydashboard)
library(shiny)
library(sodium)


credentials <- data.frame(record_id = "1", username_id = "USER", tipodeusuario = "Usuário administrador", passod = password_store("password"), logins_complete = "2")

header <- dashboardHeader(
  title = "", 
  titleWidth = 0,
  tags$li(class = "dropdown",
          tags$li(class = "dropdown", textOutput("logged_user"), style = "padding-top: 1px; padding-bottom: 1px; color: #fff;"),
          tags$li(class = "dropdown", actionLink("openModal", textOutput("logintext")))))


sidebar <- dashboardSidebar(uiOutput("sidebarpanel"))

body <- dashboardBody(shinyjs::useShinyjs(), uiOutput("body"),
                      tags$head(tags$style(HTML(
                        '.myClass { 
        font-size: 20px;
        line-height: 50px;
        text-align: left;
        font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
        padding: 0 15px;
        overflow: hidden;
        color: white;
      }
    '))))

ui <- dashboardPage(header, sidebar, body, skin = "purple")

server <- function(input, output, session){
  
  reactive_credentials <- reactiveValues(credentials = credentials)
  
  logged_in <- reactiveValues(login = FALSE)
  
  
  output$logintext <- renderText({
    if(logged_in$login) return("Logout")
    return("Login")
  })
  
observeEvent(input$login,{
    if (!(logged_in$login)) {
      if (!is.null(input$login)) {
        if (input$login > 0) {
          Username <- toupper(isolate(input$userName))
          Password <- isolate(input$passwd)
          if(length(which(reactive_credentials$credentials$username_id == Username))==1) { 
            pasmatch  <- reactive_credentials$credentials["passod"][which(reactive_credentials$credentials$username_id == Username),]
            pasverify <- password_verify(pasmatch, Password)
            if(pasverify) {
              logged_in$login <- TRUE
              removeModal()
            } else {
              shinyjs::toggle(id = "nomatch", anim = TRUE, time = 1, animType = "fade")
              shinyjs::delay(3000, shinyjs::toggle(id = "nomatch", anim = TRUE, time = 1, animType = "fade"))
            }
          } else {
            shinyjs::toggle(id = "nomatch", anim = TRUE, time = 1, animType = "fade")
            shinyjs::delay(3000, shinyjs::toggle(id = "nomatch", anim = TRUE, time = 1, animType = "fade"))
          }
        } 
      }
    }
  })
  


  observeEvent(input$openModal, {
    if(logged_in$login){
      removeModal()
      logged_in$login <- FALSE
    } else {
      showModal(
        modalDialog(title = "LOGIN",
                    textInput("userName", placeholder="Usuário", label = tagList(icon("user"), "Usuário")),
                    passwordInput("passwd", placeholder="Senha", label = tagList(icon("unlock-alt"), "Senha")),
                    
                    div(
                      style = "text-align: center; width: 500px; max-width: 100%; margin: 0 auto; padding: 20px;",
                      actionButton("login", "ENTRAR", style = "text-align: center; color: white; background-color:#7f609b;
                                 padding: 10px 15px; width: 150px; cursor: pointer;
                                 font-size: 18px; font-weight: 600;"),
                      shinyjs::hidden(
                        div(id = "nomatch",
                            tags$p("Usuário ou senha incorretos",
                                   style = "color: red; font-weight: 600; 
                                            padding-top: 5px;font-size:16px;", 
                                   class = "text-center"))),
                      br(),
                      br(),
                      br(),
                    )
        )
        
      )
    }
  })
  
}

shinyApp(ui, server)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.