Authentication
To authenticate a user you need to create a user object and then pass it to the login
function. The User object has the following properties:
parameters | description |
---|---|
userId | Unique ID for the user |
displayName | Display name of the user. Agents will identify users by this display name |
Email ID of logged in user | |
password | User's password |
imageLink | The image of the end-user, which will be visible to the agents in the kommunicate dashboard |
authenticationTypeId | Pass 1 for authentication from kommunicate |
APP_ID | Pass your APP_ID here |
deviceApnsType | 0 for development, 1 for release |
Initialize SDK
Call the initialization function once when the app starts. You don’t need to call it every time, but make sure it runs at least once before using any Kommunicate functions. Place it at the app launch entry point.
RNKommunicateChat.initialize(<KOMMUNICATE-APP-ID>, (responseArray: any[]) => {
const [initResponse, initMessage] = responseArray;
if (initResponse === 'Error') {
console.error('Initialization Error:', initMessage);
Alert.alert('Initialization Error', initMessage);
return;
}
console.log('Initialization Success:', initMessage);
});
There are 2 ways to Login
1. Visitors
Whenever users come to your app and starts the chat, you can assign them a random ID. This behaviour is best suited for anonymous user. Add below function to generate a random userId:
public getRandomId() : string {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 32; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
Then create the User object using random Id:
var kmUser = {
'userId' : this.getRandomId(),
'applicationId' : '<APP_ID>', //replace this with your APP_ID from Kommunicate Dashboard
'deviceApnsType' : 0 //Set 0 for Development and 1 for Distribution (Release)
};
Then call the loginUser
function:
RNKommunicateChat.loginUser(kmUser, (response, message) => {
if(response == 'Success') {
console.log(message);
} else if (response == 'Error') {
console.log(message);
}
});
2. Logged In Users
If the user is already logged in to your app, then pass the user details to create a user object:
var kmUser = {
'userId' : this.userId, //Replace it with the userId of the logged in user
'password' : this.password, //Put password here
'authenticationTypeId' : 1,
'imageLink' : <image-link-for-user>,
'applicationId' : '<APP_ID>', //replace this with your APP_ID from Kommunicate Dashboard
'deviceApnsType' : 0 //Set 0 for Development and 1 for Distribution (Release)
};
Then call the loginUser
function:
RNKommunicateChat.loginUser(kmUser, (response, message) => {
if(response == 'Success') {
console.log(message);
} else if (response == 'Error') {
console.log(message);
}
});
You can check if the user is logged in or not by calling the isLoggedIn
function:
RNKommunicateChat.isLoggedIn((response) => {
if(response == "True") {
//the user is logged in
} else {
//user is not logged in
}
});
Update User
You can update the user details whenever you want after the login by using below function.
let kmuser = {
'displayName' : '<name>',
'imageLink': '<image url>',
'contactNumber': '<mobile number>',
'email': '<email>',
'metadata': {
"test": "value"
}
}
RNKommunicateChat.updateConversationInfo(kmuser, (response: string, responseMessage: string) => {
console.log(response + responseMessage);
});