Log in and authentication

Learn how to authenticate customers securely with a JWT token.

The Kustomer Chat SDK does not require a user to be logged in to use the chat UI, and will automatically create a new user when the customer starts a chat session.

Without authentication, and by default, customers are only able to see their conversation history for a single device.

You can use authentication to identify customers and sync customer conversations across multiple devices.

Call Kustomer.logIn with a secure JWT (JSON Web Token) token to authenticate customers through a login, sync conversation data across different devices for the customer, and allow customers to view their chat history across their devices. See Generating a JWT token for more information.

Log in as a specific user

You can call KustomerChat.logIn to log in as a specific user. This authenticates the customer, loads their chat history, and clears any chats associated with the current customer.

Pass a hashed JWT payload in the KustomerChat.logIn method to securely identify the customer.

KustomerChat.logIn(jwtToken)
  .then((kusTrackingIdentity) => {
    // The customer is logged in.
    // kusTrackingIdentity can be used to retrieve some information
    // about the customer (See `KustomerTrackingIdentity` type)
  })
  .catch((error) => {
    // The customer was not logged in
    console.error(error);
  });

Verify if you are logged in

You can call KustomerChat.isLoggedIn(userEmail?: string, userId?: string) to see if a particular user is logged in using the email address or externalId that you passed in the logIn JWT. This function would return true if you are already logged in as the given user, and false if you are not. You can then log them in if they are not.

When picking which option to choose, use the parameter that was used to log in the last time.

KustomerChat.isLoggedIn(userEmail, userId).then((isLoggedIn) => {
  if (isLoggedIn) {
    // The customer is logged in
  } else {
    // The customer is not logged in
  }
});

Logging out

KustomerChat.logOut();

Re-authenticating (logging out then logging in again)

KustomerChat.logOutThenLogIn(jwtToken);

Generating a JWT token

See Generating a JWT token for more information.