Skip to content

Signed-in customers

If your customers sign in to your own website, such as a customer portal or your app’s help pages, your server can tell your assistants who each customer is. Then:

  • their chat belongs to their contact, when Threadkite can tell which contact they are;
  • assistants marked For signed-in customers only on the website show to them, and to nobody else;
  • they never fill in the details form when your token gives a verified email address or phone number (email_verified or phone_verified): when they ask for a person, a booking or a call back, the assistant passes it straight to your team, who reply by that email or number;
  • lookups about their own account, such as their orders, can be made for them, and only for them.

Your server proves who the customer is by signing a short-lived token with a secret only it holds. What a page passes without a signature (a name in threadkite('context'), say) is never taken as who someone is.

Owners and admins can do this.

  1. Open the website in Connections > Websites.
  2. Under Signed-in customers, click Make an identity secret.
  3. Copy the secret. We show it this once: keep it on your server, never in a page.

Make a new secret replaces it, and Stop signing in customers removes it. Either ends every signed-in chat on the website at once: customers chat as visitors until your pages send tokens signed with the new secret.

When a signed-in customer loads a page, your server signs a JWT with the identity secret:

  • Algorithm: HS256 only.
  • sub (required): your own id for the customer, up to 200 characters. Use an id that never changes, not their email address.
  • iat and exp (both required): when it was issued and when it expires, in seconds since 1970. It may last at most 24 hours; an hour is plenty. Sign a new one each time the page loads.
  • email and email_verified (optional): their email address, and true only if your system has checked they own it.
  • phone and phone_verified (optional): their phone number, and true only if you’ve checked it. For lookups by phone, send it in the international format (+447911123456).
  • name (optional): their name, up to 100 characters.

Node.js, with the jsonwebtoken package:

import jwt from 'jsonwebtoken';
const token = jwt.sign(
{
sub: customer.id,
email: customer.email,
email_verified: customer.emailConfirmed,
name: customer.name,
},
process.env.THREADKITE_IDENTITY_SECRET,
{ algorithm: 'HS256', expiresIn: '1h' },
);

Python, with PyJWT:

import os, time
import jwt
now = int(time.time())
token = jwt.encode(
{"sub": str(customer.id), "email": customer.email, "email_verified": customer.email_confirmed,
"iat": now, "exp": now + 3600},
os.environ["THREADKITE_IDENTITY_SECRET"],
algorithm="HS256",
)

PHP, with firebase/php-jwt:

use Firebase\JWT\JWT;
$now = time();
$token = JWT::encode(
['sub' => (string) $customer->id, 'email' => $customer->email, 'email_verified' => $customer->emailConfirmed,
'iat' => $now, 'exp' => $now + 3600],
getenv('THREADKITE_IDENTITY_SECRET'),
'HS256'
);

Give the token to the website script on the page, either from the page’s code:

threadkite('identify', token);

or on the script tag, as data-identity="…". The script keeps it in memory only and sends it only in the body of its requests, never in an address. When the customer signs out, tell the script, so their chat closes and the copy kept in that browser goes:

threadkite('identify', null);

A token that isn’t signed with the website’s secret, has expired, lasts more than 24 hours, has no sub, or has no iat and exp is ignored: the customer chats as a visitor. The website’s Signed-in customers card shows the last token refused and why, such as “It had expired. Sign a new one each time the page loads.”

A signed-in customer’s chat needs their current token with every message. Once it has expired (or someone else signs in), the chat ends at their next message and the widget says so; sign a new token and call threadkite('identify', token) again, and their next chat starts signed in. Keep tokens for as long as a visit lasts (an hour is plenty), and sign a new one each time the page loads.

If customers sign in to two of your websites with the same accounts (two portals, say), open the second one and, under Same sign-in as, choose the first. Tokens for the second website are then signed with the first one’s secret, and a customer is the same contact on both. Changing it ends the signed-in chats on the website you change. A website another one uses can’t itself use a third’s.

Threadkite remembers which contact each of your customer ids belongs to. The first time it sees a customer, it links them to the one contact with their verified email address or verified phone number. When there’s no such contact, the first request they pass to your team makes one from their verified email or number. When there’s more than one, it links none. With neither verified, the customer leaves their details in the chat as any visitor does. A token never changes a contact’s details.