Auth
import com.bunbase.sdk.LoginResult
when (val result = client.auth.login(email, password)) { is LoginResult.Authenticated -> { println("Logged in as ${result.result.user.email}") } is LoginResult.TotpRequired -> { // Prompt user for TOTP code, then: client.auth.verifyTotp(result.challenge.totpToken, totpCode) }}Register
Section titled “Register”val result = client.auth.register(email, password)println("Registered: ${result.user.id}")Logout
Section titled “Logout”// Revoke current sessionclient.auth.logout()
// Revoke all sessions (all devices)client.auth.logoutAll()Current user
Section titled “Current user”// Snapshotval user = client.auth.user
// Reactive (Compose)val user by client.auth.currentUser.collectAsState()
// Fetch from server (refreshes the StateFlow)val user = client.auth.me()Auth state in ViewModel
Section titled “Auth state in ViewModel”class AppViewModel : ViewModel() { val user = BunBase.client.auth.currentUser // StateFlow<AuthUser?>
fun login(email: String, password: String) { viewModelScope.launch { try { client.auth.login(email, password) } catch (e: BunBaseError.ServerError) { // handle error } } }}Password reset
Section titled “Password reset”// Step 1 — send reset emailclient.auth.forgotPassword(email)
// Step 2 — apply new password with the token from the emailclient.auth.resetPassword(token, newPassword)Email verification
Section titled “Email verification”client.auth.verifyEmail(token) // token from the verification emailclient.auth.resendVerification() // resend while authenticatedMagic link
Section titled “Magic link”// Requestclient.auth.requestMagicLink(email)
// Verify (token from the email URL)val result = client.auth.verifyMagicLink(token)TOTP / 2FA
Section titled “TOTP / 2FA”// Get current statusval enabled = client.auth.getTotpStatus()
// Begin setup — returns QR URIval setup = client.auth.setupTotp()// Show setup.otpauthUrl as QR code
// Confirm with first codeclient.auth.enableTotp(code)
// Disableclient.auth.disableTotp(code)API keys
Section titled “API keys”// Listval keys = client.auth.listApiKeys()
// Create — key is shown onceval created = client.auth.createApiKey("My key")println(created.key) // store this
// Revokeclient.auth.revokeApiKey(created.id)User metadata
Section titled “User metadata”client.auth.updateMe(mapOf("theme" to "dark", "plan" to "pro"))Delete account
Section titled “Delete account”client.auth.deleteAccount(password)