Identify signed-in users with a secure token

Show who's chatting in the Opskeet inbox by passing a signed token from your server, so chats follow users across devices.

Updated
On this page

If your site has accounts, the chat can know who's signed in. Their name, email, company and details show in the inbox, and their chats follow them across devices. A signed token proves who they are, so nobody can pretend to be someone else.

You need a backend that can sign a token. Without one, see No server below.

1. Get your secret

  1. Go to Settings → Install and find Identify signed-in users. Owners and admins see the secret.
  2. Click Create a secret, then Copy secret.
  3. Keep it in a server environment variable. Never put it in your page.

2. Make a token on your server

Sign an HS256 JWT with the secret. Settings → Install shows copy-ready code for several server languages.

ClaimNotes
user_idRequired. Their stable ID.
expRequired. At most 24 hours ahead.
email, nameOptional.
companyOptional: { id, name, domain }. Other keys, like seats, become company fields.
anything elseText, numbers and true/false become contact fields, like plan.
import jwt from "jsonwebtoken"

const token = jwt.sign(
  {
    user_id: user.id,
    email: user.email,
    name: user.name,
    plan: user.plan,
    company: { id: org.id, name: org.name, domain: org.domain },
  },
  process.env.OPSKEET_IDENTITY_SECRET,
  { algorithm: "HS256", expiresIn: "1h" }
)

3. Put it on the page

Make a fresh token on each page load and add this before the Opskeet script:

<script>
  window.OpskeetUser = { jwt: "TOKEN_FROM_YOUR_SERVER" }
</script>

After the page has loaded

Opskeet.identify({ jwt }) // someone signs in without a reload, or a fresh token on a long-open page
Opskeet.update({ plan: "pro" }) // change details, keep the token
Opskeet.logout() // on every sign-out

Always call logout when someone signs out. It ends their chat on that browser, so the next person never sees it.

If a token is bad or expired, it's refused and the browser console says why.

Require a token

Once your tokens work, turn on Require a token. Anyone without a valid token is refused. Until you do, Opskeet shows "Users arrive without a token" as a reminder.

Make a new secret

Click Make a new secret if the old one leaked. The current one stops working at once, so update your server first or signed-in users are refused.

No server

Pass details without a token:

window.OpskeetUser = {
  id: "123",
  email: "ana@example.com",
  name: "Ana",
  company: { id: "acme" },
  plan: "pro",
}

These are saved but prove nothing, so chats stay on one browser. A company only joins one you already have. Use a token when you can.

Did this help?

Related articles