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:
| Layer | How it works | Conversations belong to |
|---|---|---|
| Device ID | Automatic: made on the first launch | This install of your app |
| User identity | Opt-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:
val deviceId: Stringval client = Qwry.create(context, "YOUR_AGENT_ID")
println(client.deviceId) // a random UUID, the same on every launchidentify
class QwryClient, Package: com.qwryai.sdk
suspend fun identify(token: String): StringSends 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.
- 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.
// Node.js, with the jsonwebtoken package const token = jwt.sign({ user_id: user.id }, process.env.QWRYAI_IDENTITY_SECRET, { algorithm: "HS256", expiresIn: "1h" }); - Pass the token to the SDK
val userId = client.identify(tokenFromYourServer)Conversations now belong to this user.
- Check who the SDK is talking as
println(client.authState.value is AuthState.Identified) // true println(client.currentUserId) // the user_id from the token
Identity properties
val deviceId: StringThis install's ID. Always available.
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.
val currentUserId: String?The signed-in user's ID, confirmed by the server. null while anonymous.
AuthState
sealed interface AuthState, Package: com.qwryai.sdk
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
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.
client.logout()
println(client.authState.value) // AuthState.Anonymous
println(client.currentUserId) // null
println(client.currentConversationId) // null
println(client.deviceId) // unchanged