Skip to content

Instantly share code, notes, and snippets.

@raymondben
Last active September 19, 2022 19:24
Show Gist options
  • Select an option

  • Save raymondben/ec94181e2f26a42fb6faf6e97054d8ef to your computer and use it in GitHub Desktop.

Select an option

Save raymondben/ec94181e2f26a42fb6faf6e97054d8ef to your computer and use it in GitHub Desktop.

Revisions

  1. raymondben revised this gist Dec 1, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion server.R
    Original file line number Diff line number Diff line change
    @@ -67,7 +67,7 @@ function(input,output,session) {
    if (!is.null(things$user)) {
    ## Workaround to avoid shinyapps.io URL problems
    shinyjs::onclick("gauth_login-googleAuthUi",
    shinyjs::runjs(paste0("window.location.href='https://untangl.shinyapps.io/",this_app,"';")))
    shinyjs::runjs("window.location.href='https://yourdomain.shinyapps.io/yourapp';"))
    }
    })
    output$login <- renderUI({
  2. raymondben created this gist Dec 1, 2017.
    3 changes: 3 additions & 0 deletions global.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    library(googleAuthR)
    library(shiny)
    library(shinyjs)
    107 changes: 107 additions & 0 deletions server.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,107 @@
    function(input,output,session) {
    things <- reactiveValues(user=NULL,auth_message=NULL)

    ## configs
    local_users <- c("[email protected]") ## gmail addresses of authorized users

    options(googleAuthR.scopes.selected=c("https://www.googleapis.com/auth/userinfo.email"))
    options("googleAuthR.webapp.client_id"="your_client_id")
    options("googleAuthR.webapp.client_secret"="your_client_secret")
    ## get these values from https://console.developers.google.com/apis/credentials/oauthclient/


    ## handle authentication
    validate_user <- function(user_info,show_modal=TRUE) {
    if (show_modal) {
    showModal(modalDialog("Checking user permissions ...",
    title = "Please wait",
    fade=FALSE,footer=NULL))
    on.exit(removeModal())
    }
    things$auth_message <- tags$p("Checking user permissions ...")
    ## logged in via google. Are we an authorized user?
    if (user_info$kind=="plus#person") {
    uidx <- any(local_users %in% user_info$emails$value)
    if (any(uidx)) {
    return(local_users[uidx]) ## the matching user email address
    } else {
    ## no
    things$auth_message <- tags$p("You do not have access to this application")
    }
    } else {
    ## not a google-plus object
    things$auth_message <- tags$p("Unexpected login failure")
    }
    NULL
    }
    get_user_info <- function(id="me"){
    url <- sprintf("https://www.googleapis.com/plus/v1/people/%s",id)
    g <- googleAuthR::gar_api_generator(url,"GET")
    req <- g()
    req$content
    }
    accessToken <- callModule(googleAuth, "gauth_login",
    logout_text="Logout from Google",
    login_class="btn btn-primary",
    logout_class="btn btn-primary")
    observe({
    if (!is.null(accessToken())) {
    removeCssClass("ss-connect-dialog","myhidden")
    showModal(modalDialog("Checking user permissions ...",
    title = "Please wait",
    fade=FALSE,footer=NULL))
    user_info <- with_shiny(get_user_info,shiny_access_token=accessToken())
    val_user <- validate_user(user_info)
    removeModal()
    if (!is.null(val_user)) {
    things$user <- val_user
    }
    } else {
    ## On firefox, doing the google login shows up the "disconnected from server"
    ## box on shinyapps.io. In lieu of a decent solution, hide this
    addCssClass("ss-connect-dialog","myhidden")
    }
    })

    observe({
    if (!is.null(things$user)) {
    ## Workaround to avoid shinyapps.io URL problems
    shinyjs::onclick("gauth_login-googleAuthUi",
    shinyjs::runjs(paste0("window.location.href='https://untangl.shinyapps.io/",this_app,"';")))
    }
    })
    output$login <- renderUI({
    if (is.null(things$user)) {
    div(style="text-align:right",
    things$auth_message,
    googleAuthUI("gauth_login"))
    } else {
    tags$p(style="text-align:right","User: ",things$user)
    }
    })

    observeEvent(input$show_login,{
    showModal(modalDialog(
    title = "Log in",
    wellPanel(
    googleAuthUI("gauth_login")
    ),
    easyClose=TRUE))
    })

    observeEvent(input$do_login,{
    removeModal()
    if (!is.null(things$user)) {
    things$auth_message <- NULL
    }
    })


    output$output1 <- renderUI({
    if (is.null(things$user)) {
    tags$p("Log in for access")
    } else {
    tags$p("Hooray! You have access.")
    }
    })
    }
    8 changes: 8 additions & 0 deletions ui.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    fluidPage(useShinyjs(),
    tags$head(
    tags$style(".myhidden {display:none;}"), ## to hide selected elements
    tags$title("Shiny Google auth example")
    ),
    uiOutput(style="clear:right;","login"),
    uiOutput("output1")
    )