# 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: ```swift public nonisolated let deviceId: String ``` ```swift let client = QwryClient(agentId: "YOUR_AGENT_ID") print(client.deviceId) // a lowercase UUID, such as "3f2c…" ``` > [!NOTE] > > The device ID is a random UUID saved in `UserDefaults.standard` under the key `com.qwryai.sdk.deviceId`. It stays the same across launches and is shared by every `QwryClient` in the app. > [!WARNING] > > Because it lives in `UserDefaults`, the device ID doesn't survive a reinstall, and anonymous conversations stay with the old ID. If conversations must survive a reinstall, sign the user in with a JWT. ## 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. | 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. | **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](/docs/developer-guides/website-integration/identity-verification) for where to find the secret and how to sign tokens. > [!WARNING] > > Never put the identity verification secret in your app. Anyone can pull it out of an app bundle. Create tokens on your server and fetch them through your own API. **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. > [!NOTE] > > When sign-in succeeds, the server moves the conversations this device started anonymously to the user before `identify` returns, so history from before sign-in is kept and `listConversations()` shows it straight away. `identify` also clears `currentConversationId`, so the next `send` starts a new conversation as the user. ## 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) } ``` > [!NOTE] > > `currentUserId` is a shortcut for the user ID inside `.identified`, and `nil` while anonymous: > > ```swift > if let userId = await client.currentUserId { print(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 } ``` > [!WARNING] > > The SDK doesn't check when the token expires. An expired token stays in `authState` until a request fails with `401` `AUTH_INVALID_JWT`. Get a new token from your server and call `identify(token:)` again: > > ```swift > do { > try await client.send(text) > } catch QwryError.api(401, "AUTH_INVALID_JWT", _, _) { > try await client.identify(token: await AuthAPI.freshQwryToken()) > try await client.send(text) > } > ``` > > Calling `identify(token:)` on each launch where the user is signed in is cheap, and keeps the saved token fresh. ## 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 > [!NOTE] > > Once signed in, conversations belong to the user, so `listConversations()` returns that user's SDK conversations from every device, iOS and Android, they've signed in on. Without sign-in, conversations belong to the device. ## 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. > [!WARNING] > > `logout()` doesn't clear messages you're already showing. Replace your `ConversationState` and `ConversationListState` with new ones at the same time, so the previous user's messages aren't left on screen. ## 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) ``` ## Related > [card] Identity Verification | /docs/developer-guides/website-integration/identity-verification | Creating signed JWTs on your server. > [card] Conversations & History | /docs/ios-sdk/conversations | List conversations and load old messages. > [card] Error Handling | /docs/ios-sdk/error-handling | Handling expired and rejected tokens. > [card] Overview | /docs/ios-sdk/overview | Requirements and rate limits.