googleAuthR (version 0.6.3)

renderLogin: Render a Google API Authentication Login/logout button

Description

Use within a Shiny server.R to assign to an output for ui.R. The login button carries an ActionLink with value "signed_in" but as Shiny reloads on pushing it can't be used for detection of login state. Use !is.null(access_token()) instead.

Usage

renderLogin(session, access_token, login_text = "Login via Google",
  logout_text = "Logout", login_class = "btn btn-primary",
  logout_class = "btn btn-default", access_type = c("online", "offline"),
  approval_prompt = c("auto", "force"), revoke = FALSE)

Arguments

session

A Shiny session object

access_token

A token generated by reactiveAccessToken

login_text

What the login text will read on the button

logout_text

What the logout text will read on the button

login_class

The Bootstrap class for the login link

logout_class

The Bootstrap class for the logout link

access_type

Online or offline access for the authentication URL.

approval_prompt

Whether to show the consent screen on authentication.

revoke

If TRUE a user on logout will need to re-authenticate.

Value

An object to assign to output e.g. output$login

See Also

revokeEventObserver

Other shiny auth functions: authReturnCode, createCode, gar_shiny_getAuthUrl, gar_shiny_getToken, gar_shiny_getUrl, loginOutput, reactiveAccessToken, revokeEventObserver, with_shiny

Examples

Run this code
# NOT RUN {
## in global.R

## create the API call function, example with goo.gl URL shortner
library(googleAuthR)
options("googleAuthR.scopes.selected" = c("https://www.googleapis.com/auth/urlshortener"))

shorten_url <- function(url){

  body = list(
    longUrl = url
 )
 
 f <- gar_api_generator("https://www.googleapis.com/urlshortener/v1/url",
                        "POST",
                        data_parse_function = function(x) x$id)
                        
 f(the_body = body)
 
 }


## in server.R
library(shiny)
library(googleAuthR)
source('global.R')

shinyServer(function(input, output, session)){
  
  ## Get auth code from return URL
  access_token  <- reactiveAccessToken(session)

  ## Make a loginButton to display using loginOutput
  ## revoke=TRUE means upon logout a user will need to reauthenticate
  output$loginButton <- renderLogin(session, access_token(), revoke=TRUE)
  
  ## Needed if revoke=TRUE above
  revokeEventObserver(access_token())

  short_url_output <- eventReactive(input$submit, {
  ## wrap existing function with_shiny
  ## pass the reactive token in shiny_access_token
  ## pass other named arguments
    short_url <- with_shiny(f = shorten_url, 
                           shiny_access_token = access_token(),
                           url=input$url)
                           
   })
   
   output$short_url <- renderText({
   
     short_url_output()
     
   })
 }

## in ui.R
library(shiny)
library(googleAuthR)

shinyUI(
  fluidPage(
    loginOutput("loginButton"),
    textInput("url", "Enter URL"),
    actionButton("submit", "Shorten URL"),
    textOutput("short_url")
    ))
# }

Run the code above in your browser using DataLab