<!--
{
  "availability" : [
    "iOS: 16.4.0 -",
    "iPadOS: 16.4.0 -",
    "macCatalyst: 16.4.0 -",
    "macOS: 13.3.0 -",
    "tvOS: 16.4.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.4.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AuthenticationServices",
  "identifier" : "/documentation/AuthenticationServices/WebAuthenticationSession",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Authentication Services",
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:31_AuthenticationServices_SwiftUI03WebA7SessionV"
  },
  "title" : "WebAuthenticationSession"
}
-->

# WebAuthenticationSession

A SwiftUI environment value that views use to authenticate someone using a web service.

```
@MainActor struct WebAuthenticationSession
```

## Overview

You access an instance of this type by using the SwiftUI <doc://com.apple.documentation/documentation/SwiftUI/Environment> property wrapper and specifying <doc://com.apple.documentation/documentation/SwiftUI/EnvironmentValues/webAuthenticationSession> as the environment value.

To begin an authentication session and display the browser, call [`authenticate(using:callbackURLScheme:preferredBrowserSession:)`](/documentation/AuthenticationServices/WebAuthenticationSession/authenticate(using:callbackURLScheme:preferredBrowserSession:)). For example, when someone taps a button, the web service authenticates that person and then the authentication provider redirects the browser to a URL it constructs using the app’s custom callback scheme. The browser detects that redirect, dismisses itself, and returns the complete URL to the awaiting caller.

The following example shows how to use a SwiftUI <doc://com.apple.documentation/documentation/SwiftUI/Button> to invoke a session:

```swift
struct WebAuthenticationSessionExample: View {
    // Get an instance of WebAuthenticationSession using SwiftUI's 
    // @Environment property wrapper.
    @Environment(\.webAuthenticationSession) private var webAuthenticationSession
    
    var body: some View {
        Button("Sign in") {
            Task {
                do {
                    // Perform the authentication and await the result.
                    let urlWithToken = try await webAuthenticationSession.authenticate(
                        using: URL("https://www.example.com")!,
                        callbackURLScheme: "x-example-app"
                    )
                    // Call the method that completes the authentication using the
                    // returned URL.
                    try await signIn(using: urlWithToken)
                } catch {
                    // Respond to any authorization errors. 
                }
            }
        }
    }
}
```

After receiving the URL, inspect it to determine the authentication request’s outcome. For example, you might search the URL’s query parameters for a token of some form:

```swift
let queryItems = URLComponents(string: urlWithToken.absoluteString)?.queryItems
let token = queryItems?.filter({ $0.name == "token" }).first?.value
```

> Note:
> Refer to your authentication provider’s documentation for information on the structure of the returned URL and, if necessary, how you should parse it.

## Topics

### Authenticating a session

[`authenticate(using:callback:preferredBrowserSession:additionalHeaderFields:)`](/documentation/AuthenticationServices/WebAuthenticationSession/authenticate(using:callback:preferredBrowserSession:additionalHeaderFields:))

Begins a web authentication session.

[`BrowserSession`](/documentation/AuthenticationServices/WebAuthenticationSession/BrowserSession)

Describes the preferred browser session behavior.

[`Callback`](/documentation/AuthenticationServices/ASWebAuthenticationSession/Callback)

An object for evaluating navigation events in an authentication session.

### Deprecated methods

[`authenticate(using:callbackURLScheme:preferredBrowserSession:)`](/documentation/AuthenticationServices/WebAuthenticationSession/authenticate(using:callbackURLScheme:preferredBrowserSession:))

Begins a web authentication session.

## Relationships

### Conforms To

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Sendable`](/documentation/Swift/Sendable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)