0

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)
Matthew
  • 21
  • 1
  • 1

1 Answers1

0

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

Joe Christian
  • 11
  • 1
  • 3
  • Thanks for Joe's reply, but it does not work. I use trycatch to solve the problem. – Matthew Jan 07 '22 at 14:55
  • Thanks for Joe's suggection, but it did not work. Finally, I use trycatch function to solve this problem. ```signin_name<-"" signin_email<-"" tryCatch( { signin_name=sign_ins()$name signin_email<-sign_ins()$email } ) ``` – Matthew Jan 12 '22 at 07:53
  • Good to see it works. You may add your own answer as the answer so it might help other people someday – Joe Christian Jan 13 '22 at 02:12