Skip to content

Instantly share code, notes, and snippets.

@raymondben

raymondben/global.R

Last active Jun 27, 2021
Embed
What would you like to do?
gauth shiny example
library(googleAuthR)
library(shiny)
library(shinyjs)
function(input,output,session) {
things <- reactiveValues(user=NULL,auth_message=NULL)
## configs
local_users <- c("some_person@gmail.com") ## 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("window.location.href='https://yourdomain.shinyapps.io/yourapp';"))
}
})
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.")
}
})
}
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")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment