Skip to content

ouwargui/BetterAuthSwift

Repository files navigation

Better Auth Swift

Better Auth client for Swift

Docs


Installation

Before starting, make sure to check the Better Auth docs first.

Swift Package Manager (SPM)

Add BetterAuthSwift to your project using Xcode:

  1. In Xcode, select File → Add Package Dependencies
  2. Enter the repository URL: https://github.com/ouwargui/BetterAuthSwift.git
  3. 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
    )
]

Quick Setup

1. Initialize the client

import BetterAuth

let client = BetterAuthClient(
  baseURL: URL(string: "https://your-api.com")!
)

2. Sign In with Email

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)

3. Use in SwiftUI

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")
      }
    }
  }
}

Adding a plugin

1. Link the plugin to your target

  1. In Xcode, select File → Add Package Dependencies
  2. Enter the repository URL: https://github.com/ouwargui/BetterAuthSwift.git
  3. Click on Add package
  4. 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"),
      ]
    )
]

2. Use the plugin

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
   }

Supported plugins

Authentication

Authorization

  • Admin
  • API Key
  • MCP
  • Organization

Enterprise

  • OIDC Provider
  • SSO

Utility

  • Bearer
  • Device Authorization
  • New
  • Captcha
  • Have I Been Pwned
  • Last Login Method
  • Multi Session
  • OAuth Proxy
  • One-Time Token
  • Open API
  • JWT

3rd party

  • Stripe
  • Polar
  • Autumn Billing
  • Dodo Payments
  • Dub

Compatibility

Warning

BetterAuthSwift only supports the latest better-auth version. Please try to update before opening issues.

More information

Visit the Better Auth Swift docs for more information.

Why

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.

Disclaimer

This is a community-driven project and I'm not affiliated with Better Auth in any way.

About

Better Auth client for Swift

Resources

License

Stars

Watchers

Forks