2

I have shinyapps.io standard plan, i give access to users though their email id. https://www.shinyapps.io/

Requirements:- I want to write code that the access is removed permanently after certain time once they are successfully logged in.

App should be stopped for only one particular user whose time limit is over and should not affect other logged in users in anyways (who have logged in recently and their time limit is not over).

What I have done:-

I asked this question on rstudio community. https://community.rstudio.com/t/how-to-automatically-remove-access-permanently-to-shiny-app-after-fixed-time-once-they-are-logged-in/134564/2

Based on suggestion, i tried the below code,

library(shiny)
library(htmltools)

ui <- fluidPage(
  h1('My Shiny app'),
  p('You will be disconnected after 5 seconds...'),
  tags$script(HTML(
    "
      document.addEventListener('DOMContentLoaded', (event) => {
        setTimeout(() => {Shiny.onInputChange('disconnect', true)}, 5000)
      } )
    "
  ))
)

server <- function(input, output, session) {
  observeEvent(input$disconnect, {
    if (input$disconnect) shiny::stopApp()
  })
}

shinyApp(ui, server)

Outcome:-

While the app closes when testing on Rstudio, but it just disconnect on browser, and if you refresh the website, you get access again.

Is there anyway that once login is successful first time, after certain time access is removed permanently?

Can anyone modify the code or show any example/link where it has been discussed earlier?

Will appreciate it so much.

Thanks

amoeba
  • 21
  • 2

1 Answers1

0

You will need to set some cookies. This approach is by no means fool proof (user can delete cookies), but in the following toy example you will notice that for the first time when you launch the app, it will log out after 5 seconds and in consequent runs it will log out immediately.

library(shiny)
library(htmltools)

ui <- fluidPage(
   tags$head(
      tags$script(
         src = paste0(
            "https://cdn.jsdelivr.net/npm/js-cookie@rc/",
            "dist/js.cookie.min.js"
         )
      )
   ),
   h1('My Shiny app'),
   p('In your first run you will be disconnected after 5 seconds...'),
   p('...while in consecutive runs you will be logged out immediately'),
   tags$script(HTML(
      "
      $(document).on('shiny:connected', (event) => {
        if (Cookies.get('logon')) {
           Shiny.onInputChange('disconnect', true);
        } else {
           setTimeout(() => {Shiny.onInputChange('disconnect', true)}, 5000);
           Cookies.set('logon', true);
        }
      } )
    "
   ))
)

server <- function(input, output, session) {
   observeEvent(input$disconnect, {
      if (input$disconnect) shiny::stopApp()
   })
}

shinyApp(ui, server)
thothal
  • 16,690
  • 3
  • 36
  • 71