inst/server_side_auth_function/readme.md

This is an example of creating a Google login before your main app loads.

Wrap the call to your Shiny ui with gar_shiny_ui(ui) and then use gar_shiny_auth(session) at the top of your server function.

By default when you visit the Shiny URL when not logged in it will redirect you to the Google Login flow, but you can also setup a custom landing page with the link to login if you pass in a function that generated a UI via the login_ui to gar_shiny_ui(ui, login_ui=your_function). Two such functions are included as examples, the default login:

silent_auth <- function(req){
  shiny::tags$script(shiny::HTML(
    sprintf("location.replace(\"%s\");", gar_shiny_auth_url(req)
            )))
}

And the custom landing page shown here:

gar_shiny_login_ui <- function(req, title = "googleAuthR Login Demo"){
  check_package_loaded("shiny")
  shiny::addResourcePath("img", system.file("img", package = "googleAuthR"))
  shiny::addResourcePath("css", system.file("css", package = "googleAuthR"))  

  shiny::fillPage(
    padding = 50,
    title = title,
    shiny::tags$head(
      shiny::tags$link(rel="stylesheet",
           href="css/button.css")
    ),
    shiny::a(href = gar_shiny_auth_url(req),
      shiny::tags$button(class = "loginBtn loginBtn--google",
                        "Login with Google"
               ) 
    )
  )

}

The API calls within Shiny can then be authenticated as you would offline, since each Shiny session will carry the logged in user's credentials before the app loads.



Try the googleAuthR package in your browser

Any scripts or data that you put into this service are public.

googleAuthR documentation built on May 29, 2024, 7:22 a.m.