Created
June 14, 2019 08:40
-
-
Save frdnrdb/f59b2c1cbe4702a5899161040e347200 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -----> 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