Auth0.swift
libraries

Auth0.swift

Overview

Swift SDK for iOS, macOS, tvOS, and watchOS that lets you communicate efficiently with many of the Auth0 API endpoints and enables you to seamlessly integrate the Auth0 login.

GitHub repository ↗

Migrating from v1? Check the Migration Guide ↗

Requirements

  • iOS 12+ / macOS 10.15+ / tvOS 12.0+ / watchOS 6.2+

  • Xcode 12.x / 13.x

  • Swift 5.3+

Documentation

Installation

Swift Package Manager (SPM)

Open the following menu item in Xcode:

File > Add Packages...

In the Search or Enter Package URL search box enter this URL:

https://github.com/auth0/Auth0.swift

Was this helpful?

/

Then, select the dependency rule and press Add Package.

Cocoapods

Add the following line to your Podfile:

pod 'Auth0', '~> 2.0'

Was this helpful?

/

Then, run pod install.

Carthage

Add the following line to your Cartfile:

github "auth0/Auth0.swift" ~> 2.0

Was this helpful?

/

Then, run carthage bootstrap --use-xcframeworks.

Get started

Configuration

Auth0.swift needs the client ID and domain of the Auth0 application to communicate with Auth0. You can find these details in your Auth0 Application Settings. If you are using a custom domain, use the value of your custom domain instead of the value from the settings page.

Configure Client ID and domain with a plist

Create a plist file named Auth0.plist in your application bundle with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>ClientId</key>
    <string>YOUR_AUTH0_CLIENT_ID</string>
    <key>Domain</key>
    <string>YOUR_AUTH0_DOMAIN</string>
</dict>
</plist>

Was this helpful?

/

Configure Client ID and domain programmatically

For Web Auth

Auth0
    .webAuth(clientId: "YOUR_AUTH0_CLIENT_ID", domain: "YOUR_AUTH0_DOMAIN")
    // ...

Was this helpful?

/

For the Authentication API client

Auth0
    .authentication(clientId: "YOUR_AUTH0_CLIENT_ID", domain: "YOUR_AUTH0_DOMAIN")
    // ...

Was this helpful?

/

For the Management API client (Users)

Auth0
    .users(token: credentials.accessToken, domain: "YOUR_AUTH0_DOMAIN") // You only need the Domain
    // ...

Was this helpful?

/

Web Auth configuration (iOS / macOS)

Configure callback and logout URLs

The callback and logout URLs are the URLs that Auth0 invokes to redirect back to your application. Auth0 invokes the callback URL after authenticating the user, and the logout URL after removing the session cookie.

Since callback and logout URLs can be manipulated, you will need to add your URLs to the Allowed Callback URLs and Allowed Logout URLs fields in the settings page of your Auth0 application. This will enable Auth0 to recognize these URLs as valid. If the callback and logout URLs are not set, users will be unable to log in and out of the application and will get an error.

Navigate to your Auth0 Application Settings and add the corresponding URL to Allowed Callback URLs and Allowed Logout URLs, according to the platform of your application. If you are using a Custom Domain, replace YOUR_AUTH0_DOMAIN with the value of your Custom Domain instead of the value from the settings page.

iOS

YOUR_BUNDLE_IDENTIFIER://YOUR_AUTH0_DOMAIN/ios/YOUR_BUNDLE_IDENTIFIER/callback

Was this helpful?

/

macOS

YOUR_BUNDLE_IDENTIFIER://YOUR_AUTH0_DOMAIN/macos/YOUR_BUNDLE_IDENTIFIER/callback

Was this helpful?

/

E.g. if your iOS bundle identifier was com.company.myapp and your Auth0 Domain was company.us.auth0.com, then this value would be:

com.company.myapp://company.us.auth0.com/ios/com.company.myapp/callback

Was this helpful?

/

Configure custom URL scheme

Back in Xcode, navigate to the Info tab of your application target settings. In the URL Types section, click the  button to add a new entry. There, enter auth0 into the Identifier field and $(PRODUCT_BUNDLE_IDENTIFIER) into the URL Schemes field.

Web Auth login (iOS / macOS)

Import the Auth0 module in the file where you want to present the login page.

import Auth0

Was this helpful?

/

Present the Universal Login page in the action of your Login button.

Auth0
    .webAuth()
    .start { result in
        switch result {
        case .success(let credentials):
            print("Obtained credentials: \(credentials)")
        case .failure(let error):
            print("Failed with: \(error)")
        }
    }

Was this helpful?

/

Using async/await

do {
    let credentials = try await Auth0
        .webAuth()
        .start()
    print("Obtained credentials: \(credentials)")
} catch {
    print("Failed with: \(error)")
}

Was this helpful?

/

Using Combine

Auth0
    .webAuth()
    .start()
    .sink(receiveCompletion: { completion in
        if case .failure(let error) = completion {
            print("Failed with: \(error)")
        }
    }, receiveValue: { credentials in
        print("Obtained credentials: \(credentials)")
    })
    .store(in: &cancellables)

Was this helpful?

/

Web Auth logout (iOS / macOS)

Logging the user out involves clearing the Universal Login session cookie and then deleting the user's credentials from your application.

Call the clearSession() method in the action of your Logout button. Once the session cookie has been cleared, delete the user's credentials.

Auth0
    .webAuth()
    .clearSession { result in
        switch result {
        case .success:
            print("Session cookie cleared")
            // Delete credentials
        case .failure(let error):
            print("Failed with: \(error)")
        }
    }

Was this helpful?

/

Using async/await

do {
    try await Auth0
        .webAuth()
        .clearSession()
    print("Session cookie cleared")
    // Delete credentials
} catch {
    print("Failed with: \(error)")
}

Was this helpful?

/

Using Combine

Auth0
    .webAuth()
    .clearSession()
    .sink(receiveCompletion: { completion in
        switch completion {
        case .finished:
            print("Session cookie cleared")
            // Delete credentials
        case .failure(let error):
            print("Failed with: \(error)")
        }
    }, receiveValue: {})
    .store(in: &cancellables)

Was this helpful?

/

SSO alert box (iOS / macOS)

undefined

Check the FAQ for more information about the alert box that pops up by default when using Web Auth.

Common tasks

Learn more