Verified identity

Prove who a signed-in visitor is, so the agent can look up their own data.

A web page can claim anything. So a tool that returns personal data only trusts details your server has signed.

How it works

  1. A user is signed in on your site.
  2. Your server signs a short token that says who they are, with your workspace's secret.
  3. The page gives the token to the widget.
  4. The agent's tools can now use {visitor.external_id} and {visitor.email}.

Get the secret

Open Settings › Verified identity and press Show the secret. Only workspace owners can. Keep it on your server as an environment variable.

Never put the secret in a page or an app. Whoever has it can pose as any of your users. If it leaks, press Replace the secret: tokens signed with the old one stop working at once.

Sign a token

It is a JWT signed with HS256. Install jsonwebtoken for Node.js, pyjwt for Python or firebase/php-jwt for PHP.

import jwt from "jsonwebtoken";

export function chatToken(user) {
  return jwt.sign(
    { email: user.email, name: user.name, plan: user.plan },
    process.env.WHILEISLEEP_IDENTITY_SECRET,
    { algorithm: "HS256", subject: String(user.id), expiresIn: "1h" },
  );
}
ClaimMeaning
subRequired. Your user's id, as text. They keep their conversations on every device.
expRequired. When the token ends. An hour is plenty.
anything elseBecomes a verified attribute: email, name, plan and so on

Pass it to the widget

Print the token into the page for signed-in users only, and sign a new one on each page load:

JavaScript

function signIn() {
  window.whileisleep.identify({ token: "THE_TOKEN_FROM_YOUR_SERVER" });
}

if (window.whileisleep) signIn();
else window.addEventListener("whileisleep:ready", signIn, { once: true });

When your user signs out, call window.whileisleep.reset().

If it isn't accepted

MessageFix
The identity token has expiredSign one per page load, and check your server's clock
The identity token isn't valid for this workspaceCheck that the secret belongs to this workspace, and that the algorithm is HS256
The identity token needs a sub claimSend your user's id as text, not as a number