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:
| Level | How it works | What conversations belong to |
|---|---|---|
| Device ID | Automatic, made the first time the SDK runs | The device |
| User identity | Optional: 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:
public nonisolated let deviceId: Stringlet client = QwryClient(agentId: "YOUR_AGENT_ID")
print(client.deviceId) // a lowercase UUID, such as "3f2c…"identify
@discardableResult
public func identify(token: String) async throws -> StringSends 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.
| Parameter | Type | Required | Description |
|---|---|---|---|
token | String | Yes | An HS256 JWT your server creates and signs with the agent's identity verification secret. Its payload needs a user_id or sub claim. |
- Create a JWT on your server
Sign a JWT with the agent's identity verification secret, with the user's ID in
user_id(orsub). See Identity Verification for where to find the secret and how to sign tokens. - Pass the token to the SDK
do { let userId = try await client.identify(token: jwt) print("Signed in as \(userId)") } catch { // The token was rejected. See Error Handling. } - Check that it worked
if case .identified(let userId) = await client.authState { print("Signed in as \(userId)") }identify(token:)throws401AUTH_INVALID_JWTif 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
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)
}public private(set) var authState: AuthStateswitch 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:
let client = QwryClient(agentId: "YOUR_AGENT_ID")
if case .identified = await client.authState {
// Restored from the Keychain: already signed in
}Identity properties
| Property | Type | Description |
|---|---|---|
deviceId | String | The device ID. Always available. |
authState | AuthState | .anonymous, or .identified(userId:) after a successful identify. |
currentUserId | String? | The signed-in user's ID, or nil while anonymous. |
What identity changes
logout
public func logout()Deletes the saved token and goes back to anonymous. There's no network call.
await client.logout()
print(await client.authState) // .anonymous
print(await client.currentUserId) // nil
print(await client.currentConversationId) // nil
print(client.deviceId) // unchangedlogout() 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:
try await client.identify(token: newUserToken)
chat = ConversationState(client: client)