Skip to content
BunBase BunBase BunBase Docs Alpha v0.1.0

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)
}
}
val result = client.auth.register(email, password)
println("Registered: ${result.user.id}")
// Revoke current session
client.auth.logout()
// Revoke all sessions (all devices)
client.auth.logoutAll()
// Snapshot
val user = client.auth.user
// Reactive (Compose)
val user by client.auth.currentUser.collectAsState()
// Fetch from server (refreshes the StateFlow)
val user = client.auth.me()
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
}
}
}
}
// Step 1 — send reset email
client.auth.forgotPassword(email)
// Step 2 — apply new password with the token from the email
client.auth.resetPassword(token, newPassword)
client.auth.verifyEmail(token) // token from the verification email
client.auth.resendVerification() // resend while authenticated
// Request
client.auth.requestMagicLink(email)
// Verify (token from the email URL)
val result = client.auth.verifyMagicLink(token)
// Get current status
val enabled = client.auth.getTotpStatus()
// Begin setup — returns QR URI
val setup = client.auth.setupTotp()
// Show setup.otpauthUrl as QR code
// Confirm with first code
client.auth.enableTotp(code)
// Disable
client.auth.disableTotp(code)
// List
val keys = client.auth.listApiKeys()
// Create — key is shown once
val created = client.auth.createApiKey("My key")
println(created.key) // store this
// Revoke
client.auth.revokeApiKey(created.id)
client.auth.updateMe(mapOf("theme" to "dark", "plan" to "pro"))
client.auth.deleteAccount(password)