Skip to content
QwryAI
Documentation
Pages

Android SDK

User Identity

Sign users in with a signed token, and understand the device ID the QwryAI Android SDK keeps

Overview

The SDK knows who it's talking for in one of two ways:

LayerHow it worksConversations belong to
Device IDAutomatic: made on the first launchThis install of your app
User identityOpt-in: pass a signed token to identify()The user, on every device they sign in on

The SDK works anonymously from the start. Call identify() when a user signs in to your app.

Device ID

Every install has a stable device ID:

Kotlin
val deviceId: String
Kotlin
val client = Qwry.create(context, "YOUR_AGENT_ID")
println(client.deviceId) // a random UUID, the same on every launch

identify

class QwryClient, Package: com.qwryai.sdk

Kotlin
suspend fun identify(token: String): String

Sends the token to QwryAI to check it, signs the user in, and returns their user ID. From then on, every request is made as this user. Conversations this device started anonymously move to the user before identify() returns.

token String required

A JWT that your server signs with HS256 using your agent's identity verification Secret key. Its payload must hold a user_id (or sub) claim. See Identity verification for where to find the secret.

  1. Sign a token on your server

    Make the JWT on your own server, never in the app, so the secret never ships inside your APK.

    JavaScript
    // Node.js, with the jsonwebtoken package
    const token = jwt.sign({ user_id: user.id }, process.env.QWRYAI_IDENTITY_SECRET, { algorithm: "HS256", expiresIn: "1h" });
  2. Pass the token to the SDK
    Kotlin
    val userId = client.identify(tokenFromYourServer)

    Conversations now belong to this user.

  3. Check who the SDK is talking as
    Kotlin
    println(client.authState.value is AuthState.Identified) // true
    println(client.currentUserId)                            // the user_id from the token

Identity properties

Kotlin
val deviceId: String

This install's ID. Always available.

Kotlin
val authState: StateFlow<AuthState>

Who the SDK is talking as: AuthState.Anonymous, or AuthState.Identified(userId). Collect it to update your UI when a user signs in or out.

Kotlin
val currentUserId: String?

The signed-in user's ID, confirmed by the server. null while anonymous.

AuthState

sealed interface AuthState, Package: com.qwryai.sdk

Kotlin
sealed interface AuthState {
    data object Anonymous : AuthState                     // conversations belong to this device
    data class Identified(val userId: String) : AuthState // conversations belong to this user
}

How identity affects conversations

identify() also clears currentConversationId, so the next send starts a new conversation as the user.

logout

Kotlin
fun logout()

Forgets the token and goes back to this device's own anonymous conversations. Also clears the current conversation, so the next message starts a new one.

Kotlin
client.logout()

println(client.authState.value)        // AuthState.Anonymous
println(client.currentUserId)          // null
println(client.currentConversationId)  // null
println(client.deviceId)               // unchanged

Explore the documentation