How to set up logins with a hash using JavaScript

The instructions below are for desktops and laptops only.

Implementing logins with a hash is a great way to protect your customers' credentials from unauthorized access.


A hash is a powerful security feature that is an encrypted combination of your secret key and user ID.


In this guide, we’ll walk you through the process of setting up the login feature for your tawk.to chat widget using our API

First, get the secret key

Follow these steps to get your secret key:


1. Log in to your tawk.to account.

2. On your dashboard, select the property you need the key for. Hover over the property name to ensure you’ve selected the correct one.

3. Click the gear icon on the top menu to go to the Administration panel.

4. On the Administration menu, select Overview.

5. Scroll down to JavaScript API.

6. Enable Secure Mode.

7. Click the following icon to copy the Key:

You’ll need this key for the next section where we generate the hash.

Generate the hash

The hash is generated by a service that accepts a combination of your secret key and user ID and encrypts them into a string of characters that unauthorized parties cannot read.


You can use any encryption service of your choice to generate the hash. In this example, we show you how to do this using Crypto.js. The relevant libraries are available at:
https://cryptojs.gitbook.io/docs

https://www.npmjs.com/package/crypto-js


Follow these steps to generate the hash:

1. Copy the following code into the <head> section of your website’s HTML:

2. Copy the following function into your JavaScript file. This function handles the generation of the hash value.

3. Replace secretKey with the key copied from your tawk.to dashboard.

Next, let’s set up logins with the hash.

Set up logins with hash

Follow these steps to set up logins with hash:


1. Copy the login function from our JavaScript API into your API schema.

Note: When using the phone property with login(), ensure the phone number follows the international E.164 format (e.g., +1234567890).

2. Copy the hash you generated into the hash field.

3. Ensure the value in the userID field matches the user ID used in the encryption steps above.

After logging in, it may take up to a few seconds for your customer’s conversations to appear.

Error codes

These are the possible error codes you might encounter:

Error codeDescription
400Bad Request Error
This indicates an error in your function’s attributes. Check your code and re-send the request.
401Unauthorized Error
This indicates that
  • The user ID doesn’t match the encrypted hash value, or
  • Secure Mode isn’t enable in your tawk.to dashboard.

Check your code and re-send the request.
500Internal Server Error
This indicates our servers are currently busy. Try re-sending the request.

These error codes will appear in the developer’s console of your web browser.

Lastly, let’s see how to implement the logout function.

Logging out
Copy the logout function into your API schema:

Now that you’ve implemented logins with hash, you’ve strengthened the security of your tawk.to chat widget, giving your customers a safer, more robust experience.

Should you encounter any challenges or have further questions, please reach out to us. 


If you have feedback about this article, or if you need more help:

Was this article helpful?

9 out of 9 liked this article

Still need help? Message Us