5

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

Sushanta Deb
  • 529
  • 8
  • 20
  • 1
    Take a look here? https://cran.r-project.org/web/packages/googleAuthR/vignettes/googleAuthR.html – Rivasa May 18 '16 at 17:20
  • I am relatively new to this world so please excuse my ignorance. Based on the document below: https://developers.google.com/+/web/signin/#enable_the_google_api. , I tried to create my customer ID and secret. My app is in shinyapps.io so I entered under Authorized JavaScript origins field the following URI https://dmarket/shinyapps.io (my app link is https://dmarket.shinyapps.io/Main so wasnt sure what to enter). Now to test the login, can I test it locally in RStudio or do I deploy the code from your document access_token <- callModule(googleAuth, "loginButton") directly in the shinyapps.io – Sushanta Deb May 19 '16 at 07:00
  • You can test it locally it you add the port and the localhost to the Google project screen (e.g. 127.0.0.1:1221) - check out the setup steps in the readme https://github.com/MarkEdmondson1234/googleAuthR#google-api-setup – MarkeD Jun 21 '16 at 14:38
  • Also if you are using it just for ID purposes, check out https://github.com/MarkEdmondson1234/googleID – MarkeD Jun 21 '16 at 14:40

2 Answers2

4

There is an example in the readme, that you can see working as a Shiny app here

If you are intending it for just logging in purposes, check out GoogleID package which is built with googleAuthR with this in mind.

Example code below:

## in global.R
library(googleAuthR)
library(shiny)

options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/urlshortener")
options(googleAnalyticsR.webapp.client_id = "YOUR_PROJECT_KEY")
options(googleAnalyticsR.webapp.client_secret = "YOUR_CLIENT_SECRET")

shorten_url <- function(url){

  body = list(
    longUrl = url
  )

  f <- gar_api_generator("https://www.googleapis.com/urlshortener/v1/url",
                         "POST",
                         data_parse_function = function(x) x$id)

  f(the_body = body)

}

## server.R
source("global.R")

server <- function(input, output, session){

  ## Create access token and render login button
  access_token <- callModule(googleAuth, "loginButton")

  short_url_output <- eventReactive(input$submit, {
    ## wrap existing function with_shiny
    ## pass the reactive token in shiny_access_token
    ## pass other named arguments
    with_shiny(f = shorten_url, 
               shiny_access_token = access_token(),
               url=input$url)

  })

  output$short_url <- renderText({

    short_url_output()

  })
}

## ui.R
ui <- fluidPage(
  googleAuthUI("loginButton"),
  textInput("url", "Enter URL"),
  actionButton("submit", "Shorten URL"),
  textOutput("short_url")
)


### If the above global.R, server.R and ui.R files are in folder "test" like so:
## /home
##    |->/test/
##            /global.R
##            /ui.R
##            /server.R
##
## Port 1221 has been set in your Google Project options as the port to listen to
## as explained in authentication setup section
## run below in /home directory
shiny::runApp("./test/", launch.browser=T, port=1221)
MarkeD
  • 2,500
  • 2
  • 21
  • 35
3

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='john.doe@unkwown.com'  
    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) 
    
Ashish Baid
  • 513
  • 4
  • 9