User management
User authorization
func authorize(user: ChatUser, auth: ChatAuth = ChatAuth())
Called before opening the chat screen, once the user is known in the parent application.
Parameters:
user: AChatUserobject containing user information. (required parameter)auth:ChatAuth. Custom authorization settings (optional, agreed during integration).
User identifier requirements
The identifier field of the ChatUser object must be:
- Unique — one ID corresponds to exactly one user
- Persistent — does not change when the app is reinstalled or the session changes
- Resistant to guessing — do not use phone numbers, emails, or other publicly known data
If using such data is unavoidable, you must apply RSA encryption. For encryption questions: support@edna.io
ChatAuth parameters
| Field | Type | Required | Description |
|---|---|---|---|
token | String? | No (default nil) | Authorization token |
scheme | String? | No (default nil) | Authorization scheme |
method | ChatAuthMethod | No (default .headers) | Transmission method: .headers or .cookies |
signature | String? | No (default nil) | Signature |
isEncrypted | Bool | No (default false) | Whether signature encryption is used |
ChatUser parameters
| Field | Type | Required | Description |
|---|---|---|---|
identifier | String | Yes | Unique, persistent, guess-resistant user ID. See Identifier requirements. |
name | String? | No (default nil) | Display name shown in the operator workstation |
data | [String: String]? | No (default nil) | Arbitrary user attributes; sent to the operator workstation and usable by routing rules if the corresponding server-side settings are present. Can be updated via updateData(data:). |
ChatAuth is only needed for custom authorization (values are agreed during integration); by default call authorize(user:) without the second parameter.
Usage example:
// Create the user model
let chatUser = ChatUser(identifier: "user_uuid",
name: "Ivan Ivanov",
data: ["email": "ivan.ivanov@example.com"])
// Authorize the user in the SDK
chatCenterSDK.authorize(user: chatUser)
authorize does not return a resultThe method does not throw and returns immediately — the network request runs asynchronously. Network errors are delivered only via delegate.didReceiveNetwork(error:), so assign the delegate before calling authorize.
Switching between users
If the application supports switching between different accounts (for example, changing the client in a banking app), choose one of the scenarios below depending on whether you need to unsubscribe the previous user from push notifications on the server.
Scenario 1: Full logout (logout button, account deletion)
try chatCenterSDK?.logout()
// after successful logout() — authorize the new user
chatCenterSDK?.authorize(user: newUser)
logout() closes the session on the server and unsubscribes the device from pushes. After that — authorize(user:) for the new user. Details about asynchrony and network error scenarios — in the User logout section below.
Scenario 2: Changing the user within a session, the previous one is still valid on the server
chatCenterSDK?.deauthorizeUser()
chatCenterSDK?.authorize(user: newUser)
deauthorizeUser() clears only the local SDK state. Pushes to the previous user on this device will continue to arrive until their explicit logout() or until the server session expires. Details — in the User deletion section.
authorize with a new user without logout or deauthorizeUserA direct transition authorize(alice) → authorize(bob) without an intermediate logout()/deauthorizeUser() triggers full reconfiguration, but the session and push subscription of the previous user are not closed. Always call logout() (full exit) or deauthorizeUser() (without server-side unsubscription) before switching users.
Scenario 3: Re-authorizing the same user
| Scenario | Behavior |
|---|---|
| Nothing happened between calls | State is preserved, no repeated network requests |
deauthorizeUser() or logout() was called between calls | Full reconfiguration: WebSocket is reopened, history is reloaded |
logout() — asynchronous server-side unsubscriptionlogout() first opens a WebSocket and only on success sends the offline signal, clears the cache, and resets local state. On a network error (WebSocket failed to open), the method does not throw and does not notify the delegate, but the local state is also not cleared: the user remains authorized in the SDK, prefill is not reset, the device keeps receiving pushes. Track the failure through SDK logs and repeat logout() if necessary.
Updating user data
mutating func updateData(data: [String: String]?)
Updates user data. Sent to the server when the chat screen opens, if the WebSocket connection is active at the moment of opening.
Parameters:
data: A[String: String]?dictionary with arbitrary user attributes. Passnilto clear the data.
Usage example:
// 1. Create and authorize the user with basic data
var chatUser = ChatUser(identifier: "user_uuid",
name: "Ivan Ivanov")
chatCenterSDK.authorize(user: chatUser)
// 2. Later — update user data
chatUser.updateData(data: ["email": "ivan.ivanov@example.com",
"segment": "premium",
"city": "Moscow"])
updateData specifics- Delivery is tied to the chat controller's
viewDidAppearand only fires when the WebSocket is active. If the connection was closed, the first chat open only reopens it — data will be sent on the next chat open with an active connection. - The method is declared
mutating— theChatUservariable must bevar, notlet. CallupdateDataon the same variable you passed toauthorize— changes are immediately visible to the SDK, no repeatedauthorizeis needed.
updateData is not persisted — if the application terminates before the chat screen opens, changes are lost. To guarantee delivery, rebuild ChatUser with current values on applicationDidBecomeActive or before each authorize(user:).
Prefilling the input field
func prefill(message: String)
Sets text that will be placed into the input field the next time the chat screen is opened. The text is inserted once — on the first getChat() call after prefill, then reset.
Usage example:
// Set the prefilled message
chatCenterSDK.prefill(message: "Hello, I have a question about order #12345")
// On the next chat open, the text will be in the input field
do {
let chatController = try chatCenterSDK.getChat()
navigationController?.pushViewController(chatController, animated: true)
} catch {
print("Failed to open chat: \(error)")
}
prefill specifics- Passing an empty string clears the prefilled text.
- A repeated call to
prefill(message:)overwrites the previous value. deauthorizeUser()clears the prefilled text synchronously.logout()clears it only on successful completion of the server unsubscription — on a network error the text is preserved (see the warning aboutlogout()asynchrony).
User logout
func logout() throws(ChatCenterUIError)
Ends the session: unsubscribes the device from pushes on the server and clears local SDK state. A repeated authorize is required before opening the chat again.
Throws: ChatCenterUIError.userNotAuthorized, if the user was not authorized.
Usage example:
do {
try chatCenterSDK?.logout()
// User logged out successfully
} catch {
print("Logout error: \(error)")
}
User deletion
func deauthorizeUser()
Clears local SDK state and closes the WebSocket connection. Use this when leaving the application's authorized zone (for example, going to a PIN entry screen). A repeated authorize is required before opening the chat again.
deauthorizeUser() does not unsubscribe from pushesUnlike logout(), deauthorizeUser() does not unsubscribe the device from push notifications on the server. Pushes will continue to arrive until logout() is called or until the server session expires.
Usage example:
chatCenterSDK?.deauthorizeUser()
Related
- Showing the chat — opening the chat after authorization
- Lifecycle integration — full integration diagram
- Messages and unread counter — sending messages and the counter
- Errors reference —
ChatCenterUIErrorerrors