Logged-in users
If your user is logged in to your site and you want them to always have access to their Tori chats wherever they are logged in, pass a stable user ID and token to the element.
<tori-popup app-id="APP_ID" agent-id="AGENT_ID" user-id="user-123" token="USER_JWT" ></tori-popup>
Use the same attributes on <tori-chatbox> if you embed the inline chatbox.
Requirements
user-idmust start withuser-.- Provide
token,tokenFetcher, or both whenuser-idis present. - Do not provide
tokenwithoutuser-id. tokenFetchergets the initial token whentokenis omitted and refreshes short-lived tokens before they expire.
Generate the JWT on your server. Do not put your TalkJS secret key in browser code.
For refreshable tokens, expose an endpoint that authenticates the current user and returns a new JWT for the same user ID. Then assign it to the element:
const popup = document.querySelector("tori-popup");
popup.tokenFetcher = async () => {
const response = await fetch("https://YOUR_BACKEND/api/tori-token", { method: "POST" });
if (!response.ok) throw new Error("Could not refresh Tori token");
const { token } = await response.json();
return token;
};
You can find your secret key in the Tori dashboard.
See the TalkJS authentication docs for how to generate the token.
Typical flow
- Your user signs in to your site.
- Your backend creates a user token for that user.
- Your page renders
<tori-popup>withuser-idand either setstoken, assignstokenFetcher, or does both. - Tori uses that identity when loading the user's conversations.
Anonymous visitors
For public pages where visitors are not logged in, omit user-id and token. Tori will create and remember an anonymous visitor identity in the browser.