Swift Client
Universal Swift client for ezAuth. Zero dependencies. Uses Foundation URLSession with async/await. Requires Swift 5.9+, iOS 15+, macOS 12+.
Installation
Swift Package Manager
// Package.swift
dependencies: [
.package(url: "https://github.com/your-org/ezauth-swift.git", from: "0.1.0"),
],
targets: [
.target(name: "YourApp", dependencies: ["EZAuth"]),
]
Or in Xcode: File > Add Package Dependencies and paste the repository URL.
Initialization
import EZAuth
// Backend (secret key)
let ez = EZAuth(
baseURL: "https://auth.example.com",
secretKey: "sk_live_..."
)
// Frontend (publishable key)
let ez = EZAuth(
baseURL: "https://auth.example.com",
publishableKey: "pk_live_..."
)
Constructor Parameters
| Parameter | Type | Description |
|---|---|---|
baseURL | String | EZAuth instance URL |
secretKey | String? | Secret key for admin operations |
publishableKey | String? | Publishable key for frontend auth |
accessToken | String? | Access token for user-scoped operations |
session | URLSession | Custom URLSession (default: .shared) |
Error Handling
do {
let user = try await ez.users.get("nonexistent")
} catch let error as EZAuthError {
print(error.message) // "User not found"
print(error.status) // 404
}
ez.auth (Publishable Key)
signUp(email:password:redirectUrl:)
let result = try await ez.auth.signUp(
email: "[email protected]",
password: "s3cret"
)
// result.status, result.user_id
signIn(email:password:strategy:redirectUrl:)
// Password
let session = try await ez.auth.signIn(
email: "[email protected]",
password: "s3cret"
)
// Magic link
let result = try await ez.auth.signIn(
email: "[email protected]",
redirectUrl: "https://myapp.com/dashboard"
)
verifyCode(email:code:)
let session = try await ez.auth.verifyCode(
email: "[email protected]",
code: "482917"
)
// session.access_token, session.refresh_token
getSession()
let me = try await ez.auth.getSession()
// me.user_id, me.email, me.email_verified
signOut()
try await ez.auth.signOut()
refreshToken(_:)
let newSession = try await ez.auth.refreshToken("rt_...")
ssoExchange(token:)
let session = try await ez.auth.ssoExchange(token: "sso_token")
signInWithOAuth(provider:redirectUrl:)
let result = try await ez.auth.signInWithOAuth(
provider: "google",
redirectUrl: "https://myapp.com/callback"
)
// Open result.authorization_url in a browser/webview
ez.users (Secret Key)
list(limit:offset:email:)
let result = try await ez.users.list(limit: 10)
// result.users, result.total
create(email:password:)
let user = try await ez.users.create(email: "[email protected]")
get(_:)
let user = try await ez.users.get("user-uuid")
ez.sessions (Secret Key)
revoke(_:)
try await ez.sessions.revoke("session-uuid")
createSignInToken(userId:expiresInSeconds:)
let token = try await ez.sessions.createSignInToken(
userId: "user-uuid",
expiresInSeconds: 300
)
ez.tables
create(name:columns:)
let table = try await ez.tables.create(
name: "contacts",
columns: [
ColumnDefinition(name: "name", type: "text", required: true),
ColumnDefinition(name: "age", type: "int"),
]
)
list() / get(_:) / delete(_:)
let list = try await ez.tables.list()
let table = try await ez.tables.get("table-uuid")
try await ez.tables.delete("table-uuid")
tables.rows.insert / get / update / delete / query
let row = try await ez.tables.rows.insert(
tableId: table.id,
data: ["name": "Alice", "age": 30]
)
let results = try await ez.tables.rows.query(
tableId: table.id,
filter: ["field": "age", "op": "gte", "value": 18],
sort: SortSpec(field: "name", dir: "asc"),
limit: 50
)
ez.buckets
// Create and manage buckets
let bucket = try await ez.buckets.create(name: "avatars")
let list = try await ez.buckets.list()
// Upload and download objects
let obj = try await ez.buckets.objects.put(
bucketId: bucket.id,
key: "avatar.png",
data: imageData,
contentType: "image/png"
)
let (data, contentType) = try await ez.buckets.objects.get(
bucketId: bucket.id,
key: "avatar.png"
)
ez.storage
let tableUsage = try await ez.storage.tables()
let objectUsage = try await ez.storage.objects()
// .used_bytes, .limit_bytes, .used_percent
JSONValue
Dynamic data (row values, filter specs) uses the JSONValue enum, which supports Swift literal syntax:
let data: [String: JSONValue] = [
"name": "Alice", // .string
"age": 30, // .int
"active": true, // .bool
"score": 9.5, // .double
"tags": ["swift"], // .array
"meta": nil, // .null
]