Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Created March 19, 2016 20:20
Show Gist options
  • Save explodecomputer/ef4341872582719d6b73 to your computer and use it in GitHub Desktop.
Save explodecomputer/ef4341872582719d6b73 to your computer and use it in GitHub Desktop.
R shiny google authentication example
library(shiny)
library(shinydashboard)
library(googleAuthR)
options("googleAuthR.scopes.selected" = c("https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"))
options("googleAuthR.webapp.client_id" = "906514199468-1jpkqgngur8emoqfg9j460s47fdo2euo.apps.googleusercontent.com")
options("googleAuthR.webapp.client_secret" = "I7Gqp83Ku4KJxL9zHWYxG_gD")
user_info <- function(){
f <- gar_api_generator("https://www.googleapis.com/oauth2/v1/userinfo",
"GET",
data_parse_function = function(x) x
)
f()
}
shinyServer(function(input, output, session)
{
access_token <- reactiveAccessToken(session)
output$loginButton <- renderLogin(session, access_token())
if(is.null(shiny::isolate(access_token())))
{
output$logininfo <- renderUI({NULL})
} else {
get_user_info <- reactive({
with_shiny(f = user_info,
shiny_access_token = access_token()
)
})
output$logininfo <- renderUI({
x <- get_user_info()
HTML('<p>Logged in as <strong>', x$name, '</strong> (', x$email, ')</p>')
})
output$logged_in <- renderMenu({
menuItem("Logged in", tabName = "logged_in_page")
})
}
})
library(shiny)
library(shinydashboard)
library(googleAuthR)
shinyUI(dashboardPage(
dashboardHeader(title = "Example login"),
dashboardSidebar(
sidebarMenu(id = "tabs",
menuItem("Login", tabName = "dashboard", icon = icon("smile-o")),
menuItemOutput("logged_in")
)
),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
box(
loginOutput("loginButton"),
uiOutput("logininfo")
)
),
tabItem(tabName = "logged_in_page",
box(title = "Logged in")
)
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment