How to alert if user not log in in shiny?

66 Views Asked by At

I use the googleAuthR package in shiny, I want to alert users if they are not log in and I also want to save user's google id if they have successfully logged in. But sign_ins() is reactive consumer that I cannot do this. Any suggestions?

library(shiny)
library(googleAuthR)
library(shinyWidgets)

options(googleAuthR.webapp.client_id = "**********************")

ui <- fluidPage(
    
    titlePanel("Sample Google Sign-In"),
    
    sidebarLayout(
      sidebarPanel(
        googleSignInUI("demo")
      ),
      
      mainPanel(
        with(tags, dl(dt("Name"), dd(textOutput("g_name")),
                      dt("Email"), dd(textOutput("g_email")),
                      dt("Image"), dd(uiOutput("g_image")) ))
      )
    )
  )

server <- function(input, output, session) {
  
  sign_ins <- shiny::callModule(googleSignIn, "demo")
  
  output$g_name = renderText({ sign_ins()$name })
  output$g_email = renderText({ sign_ins()$email })
  output$g_image = renderUI({ img(src=sign_ins()$image) })
  
  if(is.null(sign_ins())){
    shinyWidgets::show_alert(title = "not log in",
                             
                             type = NULL,
                             btn_labels = "Ok")
  else{
      write.csv(sign_ins(),"file.csv")
      }

  }
}

# Run the application 
shinyApp(ui = ui, server = server)
1

There are 1 best solutions below

3
On

I'm not familiar with googleAuthR but every google-api product in R is most likely has "*_has_token" feature to validate if there is an active credential logged in the session. I've checked the googleAuthR package and i think you can use googleAuthR::gar_has_token(). So instead of

 if(is.null(sign_ins())) {...}

you can use

if(googleAuthR::gar_has_token() == FALSE){...}

to check if there is an active credentials and do your things. Hope this helpful