Skip to content

Instantly share code, notes, and snippets.

@frdnrdb
Created June 14, 2019 08:40
Show Gist options
  • Save frdnrdb/f59b2c1cbe4702a5899161040e347200 to your computer and use it in GitHub Desktop.
Save frdnrdb/f59b2c1cbe4702a5899161040e347200 to your computer and use it in GitHub Desktop.
// -----> server
server <- function(input, output, session) {
loggedIn <- callModule(authorize, 'login')
observe({
req(loggedIn())
callModule(startApp, 'dashboard')
})
}
ui <- bootstrapPage(
authorizeUI('login'),
uiOutput('dashboard')
)
shinyApp(ui, server)
// -----> authorize
library(googleAuthR)
options(googleAuthR.scopes.selected = "email")
options(googleAuthR.webapp.client_id = Sys.getenv('GOOGLE_AUTH_CLIENT_ID'))
options(googleAuthR.webapp.client_secret = Sys.getenv('GOOGLE_AUTH_CLIENT_SECRET'))
authorizeUI <- function(id) {
ns <- NS(id)
fluidPage(id = 'login',
googleAuthUI(ns('loginButton'))
)
}
authorize <- function(input, output, session) {
if (SKIP_LOGIN) return(function()TRUE)
access_token <- callModule(googleAuth, "loginButton", login_text = "Login with Google", approval_prompt = "auto", access_type = "offline", revoke = TRUE)
token <- reactiveVal(value = NULL)
loggedIn <- reactive({
req(access_token())
token(isolate(access_token()$credentials$access_token))
with_shiny(f = authReturnCode, shiny_access_token = access_token())
})
authReturnCode <- function() {
securityCode <- getOption("googleAuthR.securitycode")
pars <- shiny::parseQueryString(session$clientData$url_search)
return(ifelse(pars$state == securityCode & !is.null(token()), TRUE, FALSE))
}
return(loggedIn)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment