JohnCoene / firebase

Google FIrebase for shiny
https://firebase.john-coene.com
GNU Affero General Public License v3.0
171 stars 26 forks source link

Firebase in module not working #6

Closed ivokwee closed 3 years ago

ivokwee commented 3 years ago

Hi John,

Seems to me Firebase is not working properly in Shiny modules. See below. If I create the Firebase object outside the module and pass it as argument it seems to work. Or am I doing something wrong?

Ivo

library(shiny)
library(firebase)

FirebaseEmailPasswordUI <- function(id) {
    ns <- NS(id)
    tagList(
        textInput(ns("email"), "Your email"),
        passwordInput(ns("password"), "Your password"),
        actionButton(ns("create"), "Register"),
        actionButton(ns("signin"), "Signin"),
        plotOutput(ns("plot"))
    )
}

FirebaseEmailPasswordServer <- function(id, f) {
    moduleServer(
        id,
        function(input, output, session) 
        {
            ns <- session$ns
            if(is.null(f)) f <- FirebaseEmailPassword$new()

            ## create the user
            observeEvent(input$create, {
                cat("Registering on FireBase server...\n")
                f$create(input$email, input$password)
            })

            ## check if creation sucessful
            observeEvent(f$get_created(), {
                created <- f$get_created()                
                if(created$success){
                    showNotification("Account created!", type = "message")
                } else {
                    showNotification("Error!", type = "error")
                }
                ## print results to the console
                print(created)
            })

            observeEvent(input$signin, {
                cat("Signing-in on FireBase server...\n")        
                f$sign_in(input$email, input$password)
            })

            output$plot <- renderPlot({
                f$req_sign_in()                      
                plot(cars)
            })

            observeEvent(f$get_signed_in(), {
                ## do something
                signed <- f$get_signed_in()
                user  <- signed$response$displayName
                email <- signed$response$email
                if(signed$success) {
                    showModal(modalDialog(paste("Hello",user,"!")))
                }

            })        
        }
    )
}

ui <- fluidPage(
    useFirebase(), # import dependencies
    FirebaseEmailPasswordUI("fire123")    
)

server <- function(input, output) {
    f <- NULL
    ##f <- FirebaseEmailPassword$new()
    FirebaseEmailPasswordServer("fire123", f)
}

shinyApp(ui, server)
JohnCoene commented 3 years ago

Yes, indeed I noticed, it's an issue with the namespace of the input. I'll have to fix that eventually. For now one has to create it in the main server function.

server <- function(input, output) {
    f <- FirebaseEmailPassword$new()
    FirebaseEmailPasswordServer("fire123", f)
}
JohnCoene commented 3 years ago

Fixed, thanks for reporting this again!