Auth0 Swift

authentication-libraryAuth0iOSSwiftJWTOAuthOpenID Connectsecurity

Authentication Library

Auth0 Swift

Overview

Auth0 Swift is a dedicated iOS/macOS authentication SDK for the Auth0 platform. It enables easy implementation of Universal Login, social authentication, and multi-factor authentication (MFA) in native applications. It provides modern Swift APIs and includes standard support for integration with Keychain and Biometric authentication, with active development continuing as of 2025.

Details

Auth0 Swift is an authentication library for iOS/macOS that is fully integrated with the Auth0 platform. It offers the following key features:

  • Universal Login Integration: Seamless integration with Auth0's Hosted Login Page
  • Social Authentication: OAuth integration with providers like Google, Facebook, and Apple
  • Keychain Integration: Secure storage of credentials and Credentials Manager
  • Biometric Authentication: Passwordless authentication leveraging Touch ID and Face ID
  • Multi-Factor Authentication: MFA with push notification support through Guardian SDK integration
  • Swift Package Manager: Compatibility with modern Swift ecosystem

Pros and Cons

Pros

  • Easy setup with complete Auth0 platform integration
  • Immediate access to enterprise-grade security features
  • Integration with iOS/macOS native features (Keychain, Biometrics)
  • Connection with numerous identity providers
  • Comprehensive documentation and community support
  • Easy dependency management with Swift Package Manager

Cons

  • Difficult to use with other authentication services due to Auth0 platform dependency
  • Auth0 usage fees apply for commercial use
  • Limited cross-platform support as it's iOS/macOS specific
  • Customization limited to Auth0's feature set
  • Vendor lock-in with Auth0 ecosystem

Reference Pages

Code Examples

Basic WebAuth Setup

import Auth0

// Start Universal Login
Auth0
    .webAuth()
    .scope("openid profile offline_access")
    .audience("https://your-domain.auth0.com/userinfo")
    .start { result in
        switch result {
        case .success(let credentials):
            print("Credentials: \(credentials)")
            // Store credentials via Keychain Manager
            credentialsManager.store(credentials: credentials)
        case .failure(let error):
            print("Error: \(error)")
        }
    }

Authentication Callback Handling in AppDelegate

import Auth0

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any]) -> Bool {
    return Auth0.resumeAuth(url)
}

// For macOS
func application(_ application: NSApplication, open urls: [URL]) {
    Auth0.resumeAuth(urls)
}

Token Management with Credentials Manager

import Auth0

let credentialsManager = CredentialsManager(authentication: Auth0.authentication())

// Store credentials
credentialsManager.store(credentials: credentials)

// Retrieve valid credentials
credentialsManager.credentials { result in
    switch result {
    case .success(let credentials):
        // Use valid token
        print("Access token: \(credentials.accessToken)")
    case .failure(let error):
        // Re-authentication needed
        print("Need to re-authenticate: \(error)")
    }
}

Passwordless Authentication with Touch ID/Face ID

import Auth0
import LocalAuthentication

// Enable biometric authentication in Credentials Manager
credentialsManager.enableBiometrics(withTitle: "Touch ID", cancelTitle: "Cancel", fallbackTitle: "Use Password")

// Get token with biometric authentication
credentialsManager.credentials { result in
    switch result {
    case .success(let credentials):
        // Token retrieved after Touch ID/Face ID authentication
        print("Authenticated with biometrics")
    case .failure(let error):
        // Biometric authentication failed or token invalid
        print("Biometric auth failed: \(error)")
    }
}

User Profile Information Retrieval

import Auth0

guard let accessToken = credentials.accessToken else { return }

Auth0
    .authentication()
    .userInfo(withAccessToken: accessToken)
    .start { result in
        switch result {
        case .success(let profile):
            print("User ID: \(profile.sub)")
            print("Email: \(profile.email ?? "No email")")
            print("Name: \(profile.name ?? "No name")")
        case .failure(let error):
            print("Failed to get profile: \(error)")
        }
    }

Logout Process

import Auth0

// Logout from Auth0 (Universal Login)
Auth0
    .webAuth()
    .clearSession { result in
        switch result {
        case .success:
            print("Successfully logged out")
            // Clear local credentials as well
            credentialsManager.clear()
        case .failure(let error):
            print("Logout failed: \(error)")
        }
    }

// Clear local credentials
credentialsManager.clear()