Set the Identity

Setting the identity allows you to authorize the user.

Retrieve the configuraiton parameters

We need to have configuration for chat which we’re providing to you it means you need:

  • Region
  • Project ID
  • Client ID
  • Config ID (leave "" if you don't have it)
  • (Optional) Token secret

To connect your Sinch Chat client to your Sinch account, you need to set the following parameters in the AppDelegate file:

Parameter Description
projectID Get your project ID from your Sinch Dashboard.
clientID Get your client ID from your Sinch Dashboard.
region Use either .EU1 for Europe or .US1 for the United States.
config_id POpulate with "" if you do not have this value
secret Optional. If you have configured an optional secret token in your Sinch Dashboard, enter it here.

Setting the identity

Next you must actually authorize the user. To do so, call us this method as early as possible to authorize the user:

Copy
Copied
func setIdentity(with config: SinchSDKConfig.AppConfig, identity: SinchSDKIdentity, completion: ((Result<Void, SinchSDKIdentityError>) -> Void)? = nil)

You can call this method as many times as you want. For example:

Copy
Copied
let config = SinchSDKConfig.AppConfig(
        clientID: {{ client_id }},
        projectID: {{ project_id }},
        configID: {{ config_id }}, // If you don't have this value, leave empty string ""
        region: .EU1
)
let currentIdentityType: SinchSDKIdentity = .anonymous


SinchChatSDK.shared.setIdentity(with: config, identity: currentIdentityType) { result in

switch result {

case .success:

// SDK is authenticated successfully.

case .failure(let error):

// SDK cannot be initizalized so you can't use any functionality

}
}

Types of Sinch SDK Identity

There are two identity types. In anonymous sessions, we generate a unique User Identifier to identify the user for your projectID and clientID.

If you have a self-signed user id, you can specify that it be used in the scope of the projectID, clientID, and configID. This means you can send push notifications or start a live chat using the identifier assigned in your database.

Copy
Copied
public enum SinchSDKIdentity: Codable, Equatable {
/// Creates anonymous session with random user identifier.
case anonymous
/// Creates session with specific user identifier.
case selfSigned(userId: String, secret: String)
}

Examples

  • Anonymous session

When you call setIdentity method with .anonymous identity, a random user is generated. For example: 01GYC7B5TR5QR1NE2NRQDPGP0X. Until removeIdentity is called, this user is used for instantiating chat and push. For example:

Copy
Copied
import SinchChatSDK
//...
let currentIdentityType: SinchSDKIdentity = .anonymous

SinchChatSDK.shared.setIdentity(with: {{config}}, identity: .anonymous) { result in }
  • Signed session

In order to create a selfSign identity for user, you need to provide a unique identifier for that user. You can either use an ID, phone number, email address, or other identifier.

Note:

One benefit of using a self-signed session, as opposed to anonymous, is that you can retain your chat history when you remove and then set the same identity.

Below is an example of how to log in using your identifier:

Copy
Copied
import SinchChatSDK
//...
let userID = "{your_unique_user_id}" // for example: "user123"

let signedUserID: String = userID.hmac(algorithm: CryptoAlgorithm.SHA512, key: {your_secret})

SinchChatSDK.shared.setIdentity(with: {{config}}, identity: .selfSigned(userId: userID, secret: signedUserID)) { result in }
Note:

The your_secret value is a secret key provided to you on the Sinch Dashboard when you created Sinch Live Chat app.

Now you can use SDK functionalities like chat or push.

Remove identity

This method is used to log out users. It removes the token and other user data from the app. After calling this method, you need to call setIdentity again to use the chat and get push notifications. Below is an example of this method:

Copy
Copied
SinchChatSDK.shared.removeIdentity { result in

switch result {
case .success():
debugPrint("remove identity success")

case .failure(_):
debugPrint("remove identity error")
}
}

The next step is assign permissions.

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.