chi2labs / cognitoR

CognitoR provides easy integration of Shiny with AWS Cognito Authentication.
22 stars 10 forks source link

Flexdashboard Usage #26

Closed malnirav closed 3 years ago

malnirav commented 3 years ago

What would be the way to utilize this package within a Shiny app made using Flexdashboard? The dashboard has an ability to incorporate shiny modules directly: https://rstudio.github.io/flexdashboard/articles/shiny.html#inline-applications

In the example below, the app simply shows the spinner w/o reaching the Cognito Login UI. Likely I am doing something wrong and would appreciate some insights on what I am missing.

---
title: "test"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---

```{r global, include=FALSE}
library(flexdashboard)
library(cognitoR)
options(shiny.port = 5000)
shinyApp(
    ui = function() {
        fluidPage(
            # Load UI logout
            logout_ui("logout"),
            # Load UI Cognito.
            cognito_ui("cognito"),
            # Output to show some content.
            uiOutput("content"))
    },

    server = function(input, output, session) {

        # Call Cognito module. ####
        cognitomod <- callModule(cognito_server, "cognito")

        # Call Logout module ####
        logoutmod <- callModule(logout_server,
                                "logout",
                                reactive(cognitomod$isLogged),
                                sprintf("You are logged in as '%s'", cognitomod$userdata$email))

        # To Click on button logout of logout module, call logout in cognito module. ####
        observeEvent(logoutmod(),{
            cognitomod$logout()
        })

        # Check if user is already logged, and show a content. ####
        observeEvent(cognitomod$isLogged, {
            if (cognitomod$isLogged) {
                # User is logged
                userdata <- cognitomod$userdata
                # Render user logged.
                output$content <- renderUI({
                    p(paste("User: ", unlist(userdata$username)))
                })
            }
        })

    },

    options = list(port = 5000)
)
ppagnone commented 3 years ago

Hi @malnirav , I never had used with Flexdashboard, but I was testing and your code is working correctly with my private Cognito instance.

If you continue having problems, likely you have some problem with your AWS configuration. Did you check the ports in your AWS instance? In your shiny app side, you are using the 5000.

If configuration in shiny side is incorrect, you should to see a exception in your R console.

Please let me know any problem, Regards!

malnirav commented 3 years ago

@ppagnone Thanks for confirming code validity. I have an EC2 instance where I am running this with following open ports: 5000, 8787, 22. I think the problem resides in AWS configuration for Cognito most likely.