Better Auth client for Swift
Docs
Before starting, make sure to check the Better Auth docs first.
Add BetterAuthSwift to your project using Xcode:
- In Xcode, select File → Add Package Dependencies
- Enter the repository URL:
https://github.com/ouwargui/BetterAuthSwift.git - Select the version you want to use (always use a tag version instead of main, since main is not stable)
Or add it to your Package.swift:
dependencies: [
.package(
url: "https://github.com/ouwargui/BetterAuthSwift.git",
.upToNextMajor(from: "2.0.0") // always use a tag version instead of main, since main is not stable
)
]import BetterAuth
let client = BetterAuthClient(
baseURL: URL(string: "https://your-api.com")!
)let response = try await client.signIn.email(with: .init(
email: "user@example.com",
password: "securepassword"
))
print(response.data.user.name)
// Will be automatically updated
print(client.session.data?.session)
// So will the user
print(client.session.data?.user)import SwiftUI
import BetterAuth
@main
struct MyApp: App {
@StateObject private var authClient = BetterAuthClient(
baseURL: URL(string: "https://your-api.com")!
)
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(authClient)
.task {
// Explicitly fetch the initial session.
// future changes to the session will
// be automatically updated
await authClient.session.refreshSession()
}
}
}
}
struct ContentView: View {
@EnvironmentObject private var authClient: BetterAuthClient
var body: some View {
if let user = authClient.session.data?.user {
Text("Hello, \(user.name)")
}
if let session = authClient.session.data?.session {
Button {
Task {
try await authClient.signOut()
}
}
label: {
Text("Sign out")
}
} else {
Button {
Task {
try await authClient.signIn.email(with: .init(email: "user@example.com", password: "securepassword"))
}
}
label: {
Text("Sign in")
}
}
}
}- In Xcode, select File → Add Package Dependencies
- Enter the repository URL:
https://github.com/ouwargui/BetterAuthSwift.git - Click on Add package
- Choose the plugins you want to use and add them to your target
Or add it to your Package.swift:
dependencies: [
.package(
url: "https://github.com/ouwargui/BetterAuthSwift.git",
.upToNextMajor(from: "2.0.0")
)
],
targets: [
.target(
name: "MyApp",
dependencies: [
.product(name: "BetterAuth", package: "BetterAuthSwift"),
// Replace or add other plugins as needed
.product(name: "BetterAuthTwoFactor", package: "BetterAuthSwift"),
]
)
]import BetterAuth
// 1. Import the plugin you want
import BetterAuthTwoFactor
let client = BetterAuthClient(
baseURL: URL(string: "https://your-api.com")!,
// 2. Add it to the plugins array
plugins: [TwoFactorPlugin()]
)
if let user = client.session.data?.user,
let twoFactorEnabled = user.twoFactorEnabled {
print(twoFactorEnabled) // true or false
}- Two Factor
- Username
- Anonymous
- Phone Number
- Magic Link
- Email OTP
- Passkey
- Generic OAuth
- One Tap
- Sign In With Ethereum
- Admin
- API Key
- MCP
- Organization
- OIDC Provider
- SSO
- Bearer
- Device Authorization
- New
- Captcha
- Have I Been Pwned
- Last Login Method
- Multi Session
- OAuth Proxy
- One-Time Token
- Open API
- JWT
- Stripe
- Polar
- Autumn Billing
- Dodo Payments
- Dub
Warning
BetterAuthSwift only supports the latest better-auth version. Please try to update before opening issues.
Visit the Better Auth Swift docs for more information.
Better Auth is an awesome authentication library and after struggling a bit to create bindings for every route I decided to create this package to facilitate the life of the Swift developer.
This is a community-driven project and I'm not affiliated with Better Auth in any way.