stack/sdks/implementations/swift/Examples/StackAuthiOS
Aman Ganapathy c8694c7ff5
[Fix] [Feat] Update OAuth Sign-In and Get Token Functions to Work (#1130)
### Summary of Changes

Previously, on the Swift SDK, the `signInWithOAuth` function wasn't
working. In this PR, we fix it by having the `getOAuthUrl` function to
actually redirect correctly. Note that to do so, we updated the
`validRedirectUrl` check on the backend to accept app native redirects
(from our new trusted url scheme). Another thing to note is that we
added functionality to the `TokenStore` abstraction to conditionally
refresh the access token that the user is trying to fetch if it is
expired/close to expiring if possible. `getOAuthUrl` will attempt to get
a valid access token, and thus will rely on our algorithm documented in
`utilities.md`.

The specs serve as the source of truth.

We go further and implement Apple Native sign in. To do so, we have it
hit a new route on the backend and verify the `jwtToken` retrieved by
the sdk against an Apple-provided set of `jwks`. We use jose to do so,
in line with the rest of the codebase.

We take this opportunity to refactor the oauth provider route owing to
the amount of duplicated logic. Additionally, to enable the apple sign
in, users will have to update the Apple authentication method modal on
the dashboard and add accepted bundle ids. These are identifiers for
projects, and we will check the `JWT` on the backend to make sure the
audience is set to an accepted bundleId.

We also update the Apple modal to be more informative.

### Using the new Features

To use the Apple native sign in, users will have to 1) sign up with an
apple developer account, 2) set up their bundleids for their projects by
connecting them to the apple developer account, 3) update the Stack-Auth
Authentication Methods dashboard apple modal with the relevant fields.
Then, trying to sign in with apple with our Swift SDK will use the apple
native sign in.

### UI Changes

Renamed the fields in the apple modal. Added a new field for bundle ids.
See below.


https://github.com/user-attachments/assets/0e760c0e-3198-4818-ac7f-4900d7a125bb



Co-authored-by: Konstantin Wohlwend <n2d4xc@gmail.com>
2026-01-28 02:17:27 +00:00
..
.swiftpm/xcode/package.xcworkspace [Fix] [Feat] Update OAuth Sign-In and Get Token Functions to Work (#1130) 2026-01-28 02:17:27 +00:00
StackAuthiOS [Fix] [Feat] Update OAuth Sign-In and Get Token Functions to Work (#1130) 2026-01-28 02:17:27 +00:00
StackAuthiOS.xcodeproj [Fix] [Feat] Update OAuth Sign-In and Get Token Functions to Work (#1130) 2026-01-28 02:17:27 +00:00
README.md [Fix] [Feat] Update OAuth Sign-In and Get Token Functions to Work (#1130) 2026-01-28 02:17:27 +00:00

Stack Auth iOS Example

An interactive iOS application for testing all Stack Auth Swift SDK functions.

Prerequisites

  • Xcode 15.0 or later
  • iOS 17.0+ Simulator or device
  • Running Stack Auth backend (default: http://localhost:8102)

Running the Example

Option 1: Xcode

  1. Open the project in Xcode:

    open StackAuthiOS.xcodeproj
    
  2. Select an iOS Simulator (e.g., "iPhone 15 Pro" or any available device) as the destination

  3. Press ⌘R to build and run

Option 2: Command Line

# Build (replace device name with an available simulator on your system)
xcodebuild -scheme StackAuthiOS -destination 'platform=iOS Simulator,name=iPhone 15 Pro' build

# Build and run (opens simulator)
xcodebuild -scheme StackAuthiOS -destination 'platform=iOS Simulator,name=iPhone 15 Pro' run

Features

The app uses a tab-based interface optimized for mobile:

  • Settings: Configure API endpoint, project ID, and keys
  • Auth: Sign up, sign in, sign out, get current user
  • User: Update display name, metadata, view tokens
  • Teams: Create, list, and manage teams
  • Logs: View all SDK calls with full details (tap for more, long-press to copy)

Additional functions are accessible via navigation links in Settings:

  • Contact Channels
  • OAuth URL generation
  • Token operations
  • Server Users (admin)
  • Server Teams (admin)
  • Sessions (impersonation)

SDK Functions Covered

Client App

  • signUpWithCredential(email:password:)
  • signInWithCredential(email:password:)
  • signOut()
  • getUser() / getUser(or:)
  • getAccessToken() / getRefreshToken()
  • getAuthHeaders()
  • getOAuthUrl(provider:)

Current User

  • setDisplayName(_:)
  • update(clientMetadata:)
  • listTeams() / getTeam(id:)
  • createTeam(displayName:)
  • listContactChannels()

Server App

  • createUser(email:password:...)
  • listUsers(limit:)
  • getUser(id:)
  • createTeam(displayName:)
  • listTeams()
  • createSession(userId:)

Logging

The Logs tab shows all SDK activity in real-time:

  • Green checkmark: Successful calls with full response data
  • Red X: Errors with details
  • Blue info: In-progress calls

Tap any log entry to see full details. Long-press to copy to clipboard.

Network Configuration

For iOS Simulator to connect to your local backend:

  1. The default localhost:8102 should work in the simulator
  2. For a real device, use your computer's local IP address instead

Troubleshooting

"Could not connect to server"

  • Ensure your Stack Auth backend is running
  • Check the Base URL in Settings tab
  • For real devices, use your computer's IP instead of localhost

Build errors

  • Make sure you have Xcode 15+ installed
  • Try cleaning: Product → Clean Build Folder (⇧⌘K)