Skip to content
QwryAI
Documentation
Pages

iOS SDK

User Identity

Sign users in with a signed JWT, and understand the device ID the iOS SDK uses until they do

Out of the box the SDK is anonymous: conversations belong to the device. When your app knows who the user is, sign them in with identify(token:) and their conversations follow them to every device they sign in on.

Overview

The SDK has two levels of identity:

LevelHow it worksWhat conversations belong to
Device IDAutomatic, made the first time the SDK runsThe device
User identityOptional: you call identify(token:)The user

Every request carries the device ID in an X-Device-Id header. Once the user is signed in, requests also carry the token in X-User-Token. The SDK sets both headers for you.

Device ID

Every install gets a device ID:

Swift
public nonisolated let deviceId: String
Swift
let client = QwryClient(agentId: "YOUR_AGENT_ID")
print(client.deviceId) // a lowercase UUID, such as "3f2c…"

identify

Swift
@discardableResult
public func identify(token: String) async throws -> String

Sends a JWT to the QwryAI server to be checked, signs the user in and returns the user ID the token names. From then on, conversations belong to that user, and the token is saved so the user stays signed in after the app restarts.

ParameterTypeRequiredDescription
tokenStringYesAn HS256 JWT your server creates and signs with the agent's identity verification secret. Its payload needs a user_id or sub claim.
  1. Create a JWT on your server

    Sign a JWT with the agent's identity verification secret, with the user's ID in user_id (or sub). See Identity Verification for where to find the secret and how to sign tokens.

  2. Pass the token to the SDK
    Swift
    do {
        let userId = try await client.identify(token: jwt)
        print("Signed in as \(userId)")
    } catch {
        // The token was rejected. See Error Handling.
    }
  3. Check that it worked
    Swift
    if case .identified(let userId) = await client.authState {
        print("Signed in as \(userId)")
    }

    identify(token:) throws 401 AUTH_INVALID_JWT if the token is invalid, expired, signed with the wrong secret, or if the agent has no identity verification secret. When it throws, nothing changes: the SDK stays signed in as before, or anonymous.

AuthState

Swift
public enum AuthState: Sendable, Equatable {
    /// Conversations belong to this device.
    case anonymous
    /// Conversations belong to this user, on every device they sign in on.
    case identified(userId: String)
}
Swift
public private(set) var authState: AuthState
Swift
switch await client.authState {
case .anonymous:
    showSignInPrompt()
case .identified(let userId):
    showAccountBadge(for: userId)
}

Staying signed in

The token is saved in the Keychain (service com.qwryai.sdk.identity, one entry per agent ID), readable after the device is first unlocked. A new QwryClient reads it when it's created, so the user stays signed in across launches without calling identify again:

Swift
let client = QwryClient(agentId: "YOUR_AGENT_ID")
if case .identified = await client.authState {
    // Restored from the Keychain: already signed in
}

Identity properties

PropertyTypeDescription
deviceIdStringThe device ID. Always available.
authStateAuthState.anonymous, or .identified(userId:) after a successful identify.
currentUserIdString?The signed-in user's ID, or nil while anonymous.

What identity changes

logout

Swift
public func logout()

Deletes the saved token and goes back to anonymous. There's no network call.

Swift
await client.logout()
print(await client.authState)              // .anonymous
print(await client.currentUserId)          // nil
print(await client.currentConversationId)  // nil
print(client.deviceId)                     // unchanged

logout() also clears currentConversationId, so the next message starts a new anonymous conversation on this device.

Switching users

To switch from one user to another, sign the new user in. The new token replaces the old one, and the next message starts a new conversation:

Swift
try await client.identify(token: newUserToken)
chat = ConversationState(client: client)

Explore the documentation