% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/auth-shiny.R
\name{auth_shiny}
\alias{auth_shiny}
\alias{oauth_provider_arcgis}
\title{Authenticate with Shiny}
\usage{
oauth_provider_arcgis(host = arc_host())

auth_shiny(
  client = Sys.getenv("ARCGIS_CLIENT"),
  secret = Sys.getenv("ARCGIS_SECRET"),
  redirect_uri = Sys.getenv("ARCGIS_REDIRECT_URI"),
  host = arc_host(),
  ...
)
}
\arguments{
\item{host}{default \code{"https://www.arcgis.com"}. The host of your ArcGIS Portal.}

\item{client}{an OAuth 2.0 developer application client ID. By default uses the
environment variable \code{ARCGIS_CLIENT}.}

\item{secret}{an OAuth 2.0 developer application secret. By default uses the environment
variable \code{ARCGIS_SECRET}.}

\item{redirect_uri}{default \code{Sys.getenv("ARCGIS_REDIRECT_URI")}. The redirect URL after completing authentication flow.}

\item{...}{additional arguments passed to \code{\link[shinyOAuth:oauth_client]{shinyOAuth::oauth_client()}}}
}
\description{
\code{oath_provider_arcgis()} and \code{auth_shiny()} provide an integrated
authentication experience for \code{{shiny}} applications via the package
\href{https://github.com/lukakoning/shinyOAuth/}{shinyOAuth}. Applications that
use \code{auth_shiny()} will enable user-specific behavior and authentications.
}
\details{
Authentication with \code{auth_shiny()} requires the package \code{shinyOAuth}.
When creating an OAuth app in ArcGIS Online / Enterprise, ensure that
the app has a valid redirect URI. This \strong{must be} the same redirect URI
that is used by \code{shinyOAuth} and \strong{must be} the same port as the
shiny application is served on.

\code{oauth_provider_arcgis()} is a low-level provider function that mimics
other \verb{oauth_provider_*()} functions from \code{shinyOAuth}. Use \code{auth_shiny()}
to create an OAuth client.

\strong{Important:} The client object returned by \code{auth_shiny()} must be created
outside of the server function to ensure a shared state across the OAuth flow.
Creating the client inside the server function will result in authentication failures.

The user info returned from \code{auth$token@userinfo} has the same structure
as \code{\link[=arc_portal_self]{arc_portal_self()}}. See that function's documentation for available fields.

The below example is derived from the shinyOAuth documentation.

\if{html}{\out{<div class="sourceCode r">}}\preformatted{library(shiny)
library(shinyOAuth)
library(arcgisutils)

# Simple UI
ui <- fluidPage(
  use_shinyOAuth(),
  uiOutput("login_information")
)

client <- auth_shiny()

server <- function(input, output, session) \{
  # Set auto_redirect = FALSE for manual login
  auth <- shinyOAuth::oauth_module_server(
    "AGOL",
    client,
    auto_redirect = FALSE
  )

  output$login_information <- renderUI(\{
    if (auth$authenticated) \{
      user_info <- auth$token@userinfo
      tagList(
        tags$p("You are logged in! Your details:"),
        tags$pre(paste(capture.output(str(user_info)), collapse = "\\n"))
      )
    \} else \{
      tagList(
        tags$p("You are not logged in."),
        actionButton("login_btn", "Login with ArcGIS")
      )
    \}
  \})

  # Trigger login when button is clicked
  observeEvent(input$login_btn, \{
    auth$request_login()
  \})
\}

runApp(
  shinyApp(ui, server),
  port = 8100,
  launch.browser = FALSE
)

# Open the app in your regular browser at http://localhost:8100
}\if{html}{\out{</div>}}
}
