Skip to content
BunBase BunBase BunBase Docs Alpha v0.1.0

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)
}
let result = try await client.auth.register(email: email, password: password)
print("Registered: \(result.user.id)")
// Revoke current session
try await client.auth.logout()
// Revoke all sessions (all devices)
try await client.auth.logoutAll()
// Snapshot
let 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 server
let user = try await client.auth.me()
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
}
}
}
// Step 1 — send reset email
try await client.auth.forgotPassword(email: email)
// Step 2 — apply new password with the token from the email
try await client.auth.resetPassword(token: token, password: newPassword)
try await client.auth.verifyEmail(token: token) // token from the verification email
try await client.auth.resendVerification() // resend while authenticated
// Request
try await client.auth.requestMagicLink(email: email)
// Verify (token from the email URL)
let result = try await client.auth.verifyMagicLink(token: token)
// Status
let enabled = try await client.auth.getTotpStatus()
// Setup — returns QR URI
let setup = try await client.auth.setupTotp()
// Show setup.otpauthUrl as QR code to the user
// Confirm with first code
try await client.auth.enableTotp(code: code)
// Disable
try await client.auth.disableTotp(code: code)
// List
let keys = try await client.auth.listApiKeys()
// Create — key is shown once
let created = try await client.auth.createApiKey(name: "My key")
print(created.key) // store this immediately
// Revoke
try await client.auth.revokeApiKey(id: created.id)
try await client.auth.updateMe(metadata: ["theme": .string("dark"), "plan": .string("pro")])
try await client.auth.deleteAccount(password: password)