Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 764
Sushanta Deb Avatar asked May 18 '16 17:05

Sushanta Deb


People also ask

How do I create a shiny login page in R?

Manual steps to build authentication page. Username : myuser Password : mypass Username : myuser1 Password : mypass1 . To change them, you can edit the following code in R program. In order to modify sidebar section, you can edit the following section of code.

What is shiny proxy?

ShinyProxy is a novel, open source platform to deploy Shiny apps for the enterprise or larger organizations. Secured Embedding of Shiny Apps Since version 2.0. 1 ShinyProxy provides a REST API to manage (launch, shut down) Shiny apps and consume the content programmatically inside broader web applications or portals.

What is Shinyapps io?

Introduction. shinyapps.io is a platform for hosting {shiny} applications. Broadly, the platform places your code on computers hosted by Amazon's Web Services. These cloud computers, called instances, have R installed and are activated when a user visits the URL associated with your {shiny} application.


2 Answers

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)
like image 92
MarkeD Avatar answered Sep 17 '22 22:09

MarkeD


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) 
    
like image 34
Ashish Baid Avatar answered Sep 16 '22 22:09

Ashish Baid