User identity in the Android SDK

Attach a verified UserIdentity to the Android OctoClient so conversations follow a signed-in customer, with Kotlin code and server-side signing.

Working draft. These docs are illustrative: names, endpoints and examples describe the intended behaviour and can change before launch.

By default the SDK treats every device as an anonymous visitor. When a customer signs in to your app, you can tell the agent who they are with a UserIdentity. Conversations then follow the customer across devices, and the agent can use their name and attributes.

The UserIdentity type

data class UserIdentity(
    val userId: String,
    val userHash: String,
    val name: String? = null,
    val email: String? = null,
    val metadata: Map<String, String> = emptyMap(),
)
Field Description
userId Your own stable id for the customer.
userHash HMAC-SHA256 of userId, created on your server. Proves the id is genuine.
name, email Optional. Shown to your team in the Helpdesk.
metadata Optional key and value pairs, such as a plan name.

The fields mirror identify in the website embed. See Identity verification for the web version.

Sign the user id on your server

Your server signs the user id with the agent's identity secret and sends the hash to the app after sign-in. Never put the secret in the app.

import { createHmac } from "node:crypto"

export function userHashFor(userId) {
  return createHmac("sha256", process.env.OCTO_IDENTITY_SECRET)
    .update(userId)
    .digest("hex")
}

Create and read the secret in the agent's settings in the dashboard, then store it as a server environment variable.

Identify the user in the app

  1. After sign-in, request userId and userHash from your own backend.

  2. Call identify with both values.

    octo.identify(
        UserIdentity(
            userId = session.userId,
            userHash = session.octoHash,
            name = session.displayName,
            email = session.email,
            metadata = mapOf("plan" to "pro"),
        )
    )
    
  3. Call chat or streamChat as usual.

You should see the customer's name on the conversation in the Helpdesk if it is handed off. Later calls to conversations return that customer's history.

Sign out

Call clearIdentity when the customer signs out so the next person on the device does not see the previous history.

octo.clearIdentity()

Warning: If the hash does not match the user id, the request fails with OctoError.Unauthorized. Check that the server signs the exact same string you pass as userId.

  • Client shows how to pass an identity when you create the client.
  • Error handling explains the errors an invalid identity can cause.

Something unclear or missing? Tell us and we will fix the page.