Authentication
To use Channelize.io JavaScript SDK, user authentication with Channelize.io server is required which will return an Access Token in response. This Access Token would be used in the header of Application API calls.
Steps for Authentication
Follow the below steps for user authentication using JavaScript SDK:
Log in to Channelize.io application server using our login API call or the login methods of our SDK. This login function will provide you an Access Token in response.
Save the User ID and the issued Access Token for the user to your Application's securely managed persistent storage.
When the user tries to log in to your application, you have to load the user ID and Access Token from the storage, and then pass them to the
channelize.connect()
method.You have to initialize the Javascript SDK before connecting, by passing Public Key.
NOTE: We recommned saving logged-in user's object and Access Token in local storage so that you don’t have to verify access token every time the connect method is called.
Login / User-Authentication
To ensure that authentic users have access to Channelize.io services, Channelize.io provides two methods of Login / User-Authentication.
Authentication with User ID
This authentication method is useful for any of the below use-cases:
- GDPR and HIPAA compliant applications that don’t save or share users’ personal information like Email ID.
- Applications that have their own authentication process, and can do a token-based Single Sign-On (SSO) with Channelize.io API.
Pre-requisites
Create a Login token Callback URL in your application's backend. This should be added in the Channelize.io Dashboard under App Settings > Security. This callback URL will be invoked by the Channelize.io Login API to verify the value of pmClientServerToken used in this authentication method.
Find below the request and response for Login token Callback URL:
Request
POST https://your-token-validation-callback-url
body:
{
"token": "token",
"userId": "userId"
}
Response Body
Output JSON expected from the callback:
Send 200 status code and the below body in JSON response.
body:
{
"token": "token"
}
Steps to Implement
You just need to pass the User ID and pmClientServerToken in channelize.loginWithUserId()
method.
channelize.loginWithUserId(userId, pmClientServerToken, function (err, res) {
});
Function Params
This table lists all parameters this function supports.
Name | Type | Description |
---|---|---|
userId | String | The unique ID of the user. |
pmClientServerToken | String | User's login token that needs to be generated by your application's backend, saved inside it and deleted post user login. |
Callback Params
err
Indicates if there is an error. It will be null if there is no error.res
Indicates the response having access token and login user objects.
Response Object Keys
id
Indicates the accessToken.user
Indicates the login user's object. You can run various methods on this object. These methods are listed and explained in the user's section.
Authentication with Email and Password
This authentication method is useful for below use-cases:
- Standalone Chat Applications built with Channelize.io.
- Applications which do not have their own authentication process, and want to use Channelize.io API for primary user authentication as well.
Steps to Implement
You just need to pass Email and Password in channelize.loginWithEmailPassword()
method.
channelize.loginWithEmailPassword(email, password, function (err, res) {
});
Function Params
This table lists all parameters this function supports.
Name | Type | Description |
---|---|---|
String | The unique email address of the user. | |
password | String | The login password of the user. |
Callback Params
err
Indicates if there is an error. It will be null if there is no error.res
Indicates the response having Access Token and login user objects.
Response Object Keys
id
Indicates the Access Token.user
Indicates the login user's object. You can run various methods on this object. A list of these methods will be explained in the user's section.
Connect
To establish a connection with the Channelize.io server you need to pass UserID & Access Token to Channelize.connect()
method. Once the connection is established, you will be able to call SDK’s methods and register event callbacks without any error.
var channelize = new Channelize.client({publicKey: YOUR_PUBLIC_KEY});
channelize.connect(userId, accessToken, function (err, res) {
});
Function Params
This table lists all parameters this function supports.
Name | Type | Description |
---|---|---|
userId | String | The unique ID of the user. |
accessToken | String | The accessToken of the user. |
Callback Params
err
Indicates if there is any error. It will be null if there is no error.res
Indicates the response of the connect function and will be true if the connection is established successfully.
Disconnect
Disconnect the user from the Channelize.io server by simply passing the user's Device ID to the Channelize.disconnect()
method. Post disconnection, you will not be able to call SDK’s methods and will not receive event callbacks from the Channelize.io server.
deviceId
is registered when you register push notification token by using push notification token API call. It is unique for every mobile device as well as a web browser.
channelize.disconnect(deviceId, function (err, res) {
});
Function Params
This table lists all possible parameters this function supports.
Name | Type | Description |
---|---|---|
deviceId | String | Specifies the device ID. This device ID should be the unique id of your device (Android/iOS OR Web Browser). If you do not have device ID, you can send it null. |
Callback Params
err
Indicates if there is any error. It will be null if there is no error.res
Indicates the response. It will be empty for this method.