Search code examples
rgoogle-authenticationshiny-servershiny

ShinyApp Google Login


I have a shinyapp and I want to enable certain features to the members who login to the app using google login. I am not able to implement the Google login and authentication process within my app using the GoogleAuthR package. Does anyone has an example of a sample ShinyApp which allows the audience to login through either google or any other social forum authorizations

Appreciate a demo with code.

PS: I have no intention of running statistics on Google data but I only want to do away with the hassle of creating a login module for my app and let Google login take care of the hassles

Thank you SD


Solution

  • I solved this problem in a different manner using gar_shiny_ui

    1. we need to define the UI in the server

    2. Get user info and extract email from his/her google login

    3. Use this email to determine if the person is from your organisation

    4. If the person is from your organisation, show the main UI else show a UI which says 'You cannot access this tool'

      #Function to get google user data which will be used for checking if the user comes from your organisation
      user_info <- function(){
      f <-                         gar_api_generator("https://www.googleapis.com/oauth2/v1/userinfo",
                           "GET",
                           data_parse_function = function(x) x)
      f()}
      
      #UI code based on Output coming via server code
      ui<-uiOutput('myUI')
      
      #Server side code to do all the lifting
      server = function(input, output,session) {
      gar_shiny_auth(session)
      
      #Check if user has already logged in with google authentication
      gmail='[email protected]'  
      tryCatch({
        x<- user_info()
        gmail=x$email
        print(gmail)}) 
      print(gmail)
      
      #Create a different UI based on where the user comes from (MyOrg or Not)
      output$myUI <- renderUI({
      
        if(grepl('@myorganisation.com',gmail)){
          ui = fluidPage(
            shinyjs::useShinyjs(),
            title='Your Product',
            theme = shinytheme("cerulean"),
            img(src = "mycompany_logo.png", height = 200, width = 400),
      
            sidebarLayout(
              sidebarPanel(write whatever you want)
              ,
              mainPanel( write whatever you want)
            )
          )} 
        else {
          ui = fluidPage(mainPanel(
            h4("My Company Data Team Presents", allign="center"),
            h1("My Tool", allign="center"),
            p("Tool that makes analysing any and everything ",
              em("incredibly easy "),
              "with a simple click."),
            br(),
            p("- But unfortunately, your account does not have the rights to ",
              em("access "),
              "this tool.")))  }
      })
      shinyApp(gar_shiny_ui(ui, login_ui = silent_auth), server)