Loading...
Loading...
Use when adding Auth0 authentication to an iOS, macOS, tvOS, watchOS, or visionOS application — integrates the Auth0.swift SDK for native Apple platform authentication using Web Auth, CredentialsManager, and biometric protection.
npx skill4agent add auth0/agent-skills auth0-swiftCredentialsManagerAgent instruction: Before providing SDK setup instructions, fetch the latest release version by running:bashgh api repos/auth0/Auth0.swift/releases/latest --jq '.tag_name'Use the returned version in all dependency lines instead of any hardcoded version below. Current known version:.2.18.0
brew install auth0/auth0-cli/auth0Agent instruction: Follow these steps in order. If you encounter an error at any step, attempt to fix it up to 5 times before callingto ask the user for guidance. Always search existing code first — if there are existing login/logout handlers, hook into them rather than creating new ones.AskUserQuestion
Agent instruction: Check the project directory for an existing package manager file:
present → CocoaPodsPodfile present → CarthageCartfile present → Swift Package ManagerPackage.swiftIf none are found, ask via: "Which dependency manager does your project use — Swift Package Manager, CocoaPods, or Carthage?"AskUserQuestionSwift Package Manager —project: Run this command in the project root to add the dependency automatically, then addPackage.swiftto the target's"Auth0"array independencies:Package.swiftbashswift package add-dependency https://github.com/auth0/Auth0.swift --from 2.18.0Swift Package Manager — Xcode project (, no.xcodeproj): The CLI command does not apply. Instruct the user to add the package via Xcode: File → Add Package Dependencies →Package.swift→ Up to Next Major Version fromhttps://github.com/auth0/Auth0.swift.2.18.0CocoaPods or Carthage: Follow the matching installation steps in Setup Guide. Do not just show the instructions — perform the file edits and run the commands.
Agent instruction:
- If Auth0 credentials (domain AND client ID) are already in the user's prompt: Write
directly with those values and proceed to Step 3.Auth0.plist- If no credentials are provided: Run the bootstrap script — do NOT ask the user to create or configure an Auth0 application manually. Always use the CLI path.
Follow Setup Guide — Auth0 Configuration for pre-flight checks and the script command.
Agent instruction:
- Read
to obtainAuth0.plistandClientId.Domain- Extract the bundle identifier from
: search forproject.pbxproj, skip values containingPRODUCT_BUNDLE_IDENTIFIERor$(.Tests- Ask the user via
: "Which callback URL scheme would you like to use?"AskUserQuestion
- Custom scheme (
) — simpler, works on all Apple platforms{bundle}://- HTTPS Universal Links — recommended for production; prevents URL scheme hijacking
Then follow only the matching path below.
Agent instruction: Register the callback URLs using the Auth0 CLI (substitute real values for,CLIENT_ID,BUNDLE_ID):DOMAINbashauth0 apps update CLIENT_ID \ --callbacks "BUNDLE_ID://DOMAIN/ios/BUNDLE_ID/callback" \ --logout-urls "BUNDLE_ID://DOMAIN/ios/BUNDLE_ID/callback" \ --no-inputThen follow the URL scheme registration steps in Setup Guide to registeras a URL type in Xcode.$(PRODUCT_BUNDLE_IDENTIFIER)
Agent instruction: All four steps below are required — skipping any one will cause the callback redirect to fail silently after login.Step B1 — Register callback URLs via Auth0 CLI: Register both HTTPS and custom scheme so the app works in all scenarios:bashauth0 apps update CLIENT_ID \ --callbacks "https://DOMAIN/ios/BUNDLE_ID/callback,BUNDLE_ID://DOMAIN/ios/BUNDLE_ID/callback" \ --logout-urls "https://DOMAIN/ios/BUNDLE_ID/callback,BUNDLE_ID://DOMAIN/ios/BUNDLE_ID/callback" \ --no-inputStep B2 — Configure Device Settings via Auth0 CLI: ExtractfromDEVELOPMENT_TEAM(10-character value, e.g.project.pbxproj). If not found, ask viaABC12DE34F: "What is your Apple Team ID? (developer.apple.com → Account → Membership Details)"AskUserQuestionbashauth0 api patch applications/CLIENT_ID \ --data '{"mobile":{"ios":{"team_id":"TEAM_ID","app_bundle_identifier":"BUNDLE_ID"}}}' \ --no-inputAuth0 will now hostautomatically — required for Universal Links to work on device.https://DOMAIN/.well-known/apple-app-site-associationStep B3 — Add Associated Domains entitlement in Xcode: Addto the app'scom.apple.developer.associated-domainsfile with both.entitlementsandapplinks:entries for the Auth0 domain. See Setup Guide — Associated Domains for the complete entitlements XML, Xcode capability steps, and build settings verification.webcredentials:Step B4 — Usein the SDK:.useHTTPS()swiftAuth0.webAuth().useHTTPS()
Agent instruction: Search the project for(SwiftUI) or@main struct/AppDelegate(UIKit) to detect the UI framework. If ambiguous, ask viaUIViewController: "Does your app use SwiftUI or UIKit?" Then follow only the matching path below.AskUserQuestion
Agent instruction: Createas anAuthenticationService.swift, then wire it into the app entry point and root view. Search for theObservableObjectstruct and@main(or equivalent root view) and update them as shown.ContentView
// AuthenticationService.swift
import Auth0
import Combine
class AuthenticationService: ObservableObject {
@Published var isAuthenticated = false
private let credentialsManager = CredentialsManager(authentication: Auth0.authentication())
init() { isAuthenticated = credentialsManager.canRenew() }
func login() async {
do {
let credentials = try await Auth0
.webAuth()
.useHTTPS()
.scope("openid profile email offline_access")
.start()
_ = credentialsManager.store(credentials: credentials)
await MainActor.run { isAuthenticated = true }
} catch WebAuthError.userCancelled { }
catch { print("Login failed: \(error)") }
}
func logout() async {
do { try await Auth0.webAuth().useHTTPS().clearSession() }
catch { print("Logout failed: \(error)") }
_ = credentialsManager.clear()
await MainActor.run { isAuthenticated = false }
}
}// @main App struct — inject AuthenticationService as environment object
@StateObject private var auth = AuthenticationService()
// In body: ContentView().environmentObject(auth)
// Root ContentView — branch on authentication state
@EnvironmentObject var auth: AuthenticationService
// In body: if auth.isAuthenticated { HomeView() } else { LoginView() }Agent instruction: Createas a plain class, then add login/logout calls to the relevantAuthenticationService.swift. Also check whether the app usesUIViewController— if so, addSFSafariViewControllertoWebAuthentication.resume(with:)/AppDelegate(see note below).SceneDelegate
// AuthenticationService.swift
import Auth0
class AuthenticationService {
private let credentialsManager = CredentialsManager(authentication: Auth0.authentication())
var isAuthenticated: Bool { credentialsManager.canRenew() }
func login() async throws {
let credentials = try await Auth0
.webAuth()
.useHTTPS()
.scope("openid profile email offline_access")
.start()
_ = credentialsManager.store(credentials: credentials)
}
func logout() async throws {
try await Auth0.webAuth().useHTTPS().clearSession()
_ = credentialsManager.clear()
}
}// In your UIViewController
private let auth = AuthenticationService()
@IBAction func loginTapped(_ sender: UIButton) {
Task {
do {
try await auth.login()
await MainActor.run { navigateToHome() }
} catch WebAuthError.userCancelled { }
catch { print("Login failed: \(error)") }
}
}
@IBAction func logoutTapped(_ sender: UIButton) {
Task {
do { try await auth.logout() }
catch { print("Logout failed: \(error)") }
await MainActor.run { navigateToLogin() }
}
}Note — SFSafariViewController only: If the app usesinstead of the default.provider(WebAuthentication.safariProvider()), addASWebAuthenticationSessiontoWebAuthentication.resume(with: url)andAppDelegate.application(_:open:url:options:). See Integration Patterns for the exact code.SceneDelegate.scene(_:openURLContexts:)
Agent instruction: Run a build to verify the integration compiles without errors:bashxcodebuild build -scheme YOUR_SCHEME -destination "platform=iOS Simulator,name=iPhone 16"If the build fails, review error messages and fix up to 5 times before asking the user.
| Mistake | Fix |
|---|---|
| Auth0 app type not set to Native | In Auth0 Dashboard, select "Native" when creating the application |
| Missing callback URL in Auth0 Dashboard | Add both |
| Right-click file in Navigator → "Add Files to Target" → check your app target |
Missing | Add |
Tokens stored in | Always use |
Calling | Store credentials from login result before attempting to retrieve |
Opening | Always open the |
Not calling | Always call |
| Build error "No such module 'Auth0'" | Verify the package is added to the correct target; for CocoaPods, open |
auth0-quickstartauth0-cli