# Recognise your signed-in users in the web chat

Sign a short token on your server so the Brigg web chat knows which of your users is writing, and shows them their own earlier conversations.

The Brigg web chat is anonymous by default: a visitor is whoever happens to hold the browser. When the chat sits inside your own signed-in app, your server can vouch for the user instead. The chat then knows who is writing and which company they belong to without asking, files the case on the right contact and company, and shows a returning user their own earlier conversations on any device.

Recognising signed-in users is switched on per workspace while it is new. Ask Brigg to switch it on for yours.

## How it works

1. You create a key pair. The private key stays on your server; an administrator pastes the public key into Brigg under Customer service, the inbox's web chat channel.
2. When one of your users is signed in, your server signs a short-lived token saying who they are.
3. Your page hands that token to the chat with `BriggChat("boot", { token })`.
4. Brigg verifies the token against your public key. A token that does not verify removes the chat from the page; it never falls back to an anonymous chat.

Brigg holds only your public key, so nothing in Brigg can produce a token for one of your users.

## Create the key pair

The key is ECDSA on the P-256 curve (the `ES256` algorithm).

```bash
openssl ecparam -name prime256v1 -genkey -noout -out brigg-chat-private.pem
openssl ec -in brigg-chat-private.pem -pubout -out brigg-chat-public.pem
```

Keep `brigg-chat-private.pem` with your server's other secrets. Paste the contents of `brigg-chat-public.pem` into Brigg. The same page shows the audience value your tokens must carry.

## Sign a token on your server

Sign the token where your own session is checked, never in the browser.

| Claim | Required | Meaning |
| --- | --- | --- |
| `sub` | yes | Your own stable id for the user. It must never be reused for another person, and it must be unique across every key registered on the inbox. |
| `email` | yes | The user's e-mail address. |
| `name` | no | The user's name. |
| `company` | no | `{ "id": "...", "name": "..." }`: your own stable id for the user's company, and its name. |
| `aud` | yes | The audience shown beside the key in Brigg. It names one inbox in one workspace. |
| `iat` | yes | When the token was signed, in seconds. |
| `exp` | yes | When it expires, in seconds. At most ten minutes after `iat`. |

```js
import jwt from "jsonwebtoken";

const token = jwt.sign(
  {
    sub: user.id,
    email: user.email,
    name: user.name,
    company: { id: company.id, name: company.name },
  },
  process.env.BRIGG_CHAT_PRIVATE_KEY,
  { algorithm: "ES256", audience: process.env.BRIGG_CHAT_AUDIENCE, expiresIn: "5m" },
);
```

Brigg matches a user on `sub` first, so a user who changes e-mail address stays the same contact. The first time a `sub` is seen, the user is matched to an existing contact by `email`. A company is matched on `company.id`, then on its exact name.

## Tell the chat who is signed in

Add this small stub before the chat's script tag. It lets your page call `BriggChat` before the script has loaded.

```html
<script>
  window.BriggChat = window.BriggChat || function () {
    (BriggChat.q = BriggChat.q || []).push(arguments);
  };
</script>
<script src="https://brigg.app/web-chat.js" data-brigg-org="o_your_workspace" data-brigg-inbox="support" async></script>
```

A page with a strict content security policy can put the stub in its own script file.

There are three commands.

| Command | When | What it does |
| --- | --- | --- |
| `BriggChat("boot", { token })` | The user is signed in. | Signs that user in to the chat. The launcher appears once the token is verified; the panel opens when the user clicks it or you call `open`. Call `boot` on every page load with a freshly signed token, and again whenever your app renews its own session. |
| `BriggChat("shutdown")` | The user signs out. | Closes the chat and drops the signed-in session. Nothing about a signed-in user is ever stored in the browser, so the next person at it sees none of their conversations. It also clears an anonymous conversation the browser stored earlier. |
| `BriggChat("open")` | Any time. | Opens the chat panel. |

Call `shutdown` before you sign a different user in on the same browser, then `boot` with the new user's token.

The chat keeps the signed-in session in memory only and for at most twelve hours. An app that stays open longer should call `boot` again with a fresh token; until it does, the chat tells the user to reload the page.

## What a signed-in user can read

A signed-in user reads only conversations that were opened with their own `sub` in that inbox. Sharing an e-mail address or a company with another user never opens that user's conversations.

The `sub` is matched per inbox, not per key. If two of your systems sign tokens for the same inbox, give their users ids that cannot collide, or use one inbox per system.

## Keep the token safe

The token is a bearer: whoever holds it can sign in as that user until it expires. Brigg does not limit it to one use.

- Sign it for a few minutes only, and never for longer than ten.
- Hand it to the page over HTTPS and do not log it.
- There is no way to end one user's chat session from your server. The session ends when the page is reloaded or closed, after twelve hours at the latest, or when you remove the key, which ends every session it opened.

## Replace or withdraw a key

To replace a key, add the new public key in Brigg, deploy the new private key on your server, then remove the old public key. Both keys verify while they are both listed.

Removing a key ends every chat session it opened at that session's next request. A conversation that is open and streaming at that moment can keep receiving replies for up to two hours, but nothing new can be opened or sent. The conversations stay in Brigg.

## When a token is refused

`boot` writes the reason to the browser console.

| Reason | Meaning |
| --- | --- |
| `no_key` | No public key is registered on this inbox. |
| `bad_signature` | The token was not signed with a registered key. |
| `wrong_audience` | `aud` is not this inbox's audience. |
| `expired` | `exp` has passed. Brigg allows 60 seconds of clock difference. |
| `not_yet_valid` | `iat` is more than 60 seconds in the future. Check your server's clock. |
| `lifetime_too_long` | `exp` is more than ten minutes after `iat`. |
| `bad_claims` | A required claim (`sub`, `email`, `aud`, `iat`, `exp`) is missing or malformed, or `name` or `company` has the wrong shape. |
| `malformed` | The token is not an `ES256` token. |

