Auth
switch try await client.auth.login(email: email, password: password) {case .authenticated(let result): print("Logged in as \(result.user.email)")case .totpRequired(let challenge): // Prompt for TOTP code, then: try await client.auth.verifyTotp(totpToken: challenge.totpToken, code: totpCode)}Register
Section titled “Register”let result = try await client.auth.register(email: email, password: password)print("Registered: \(result.user.id)")Logout
Section titled “Logout”// Revoke current sessiontry await client.auth.logout()
// Revoke all sessions (all devices)try await client.auth.logoutAll()Current user
Section titled “Current user”// Snapshotlet user = client.auth.currentUser
// Combine publisher (fires on any thread — dispatch to main for UI)client.auth.currentUserPublisher .receive(on: RunLoop.main) .sink { user in updateUI(user) } .store(in: &cancellables)
// Fetch from serverlet user = try await client.auth.me()Auth state in SwiftUI
Section titled “Auth state in SwiftUI”class AppViewModel: ObservableObject { @Published var user: AuthUser? private var cancellables = Set<AnyCancellable>()
init() { BunBaseClient.shared.auth.currentUserPublisher .receive(on: RunLoop.main) .assign(to: &$user) }
func login(email: String, password: String) async { do { _ = try await BunBaseClient.shared.auth.login(email: email, password: password) } catch { // handle error } }}Password reset
Section titled “Password reset”// Step 1 — send reset emailtry await client.auth.forgotPassword(email: email)
// Step 2 — apply new password with the token from the emailtry await client.auth.resetPassword(token: token, password: newPassword)Email verification
Section titled “Email verification”try await client.auth.verifyEmail(token: token) // token from the verification emailtry await client.auth.resendVerification() // resend while authenticatedMagic link
Section titled “Magic link”// Requesttry await client.auth.requestMagicLink(email: email)
// Verify (token from the email URL)let result = try await client.auth.verifyMagicLink(token: token)TOTP / 2FA
Section titled “TOTP / 2FA”// Statuslet enabled = try await client.auth.getTotpStatus()
// Setup — returns QR URIlet setup = try await client.auth.setupTotp()// Show setup.otpauthUrl as QR code to the user
// Confirm with first codetry await client.auth.enableTotp(code: code)
// Disabletry await client.auth.disableTotp(code: code)API keys
Section titled “API keys”// Listlet keys = try await client.auth.listApiKeys()
// Create — key is shown oncelet created = try await client.auth.createApiKey(name: "My key")print(created.key) // store this immediately
// Revoketry await client.auth.revokeApiKey(id: created.id)User metadata
Section titled “User metadata”try await client.auth.updateMe(metadata: ["theme": .string("dark"), "plan": .string("pro")])Delete account
Section titled “Delete account”try await client.auth.deleteAccount(password: password)