---
title: "JavaScript SDK: User identity"
description: "Identify customers and attach user information with the Gleap JavaScript SDK. Follow the integration code examples."
url: https://docs.gleap.ai/en/articles/395-javascript-sdk-user-identity
collection: "JavaScript"
updated: 2026-11-16
language: en
---

# JavaScript SDK: User identity

Identify customers and attach user information with the Gleap JavaScript SDK. Follow the integration code examples.

Signing in a user ties them to their feedback items. Gleap stores a unique user ID and optional meta information such as their email and name on the contact.

### Sign in your users

Sign your users in with an **identity token**: a short JWT that your backend signs for the logged-in user. Gleap checks the token once and answers with a session grant that the SDK sends with every later request. Everything in the token is signed: the user ID, the email, the name, the company and any attribute you add, such as `stripeCustomerId`.

1.  Create an identity key in **Settings → Security → Customer identity → Identity keys**. Keep its secret on your server.
    
2.  After login, your backend signs a token for the user with that key.
    
3.  Your app passes the token to `Gleap.setUserJwt()` after login and on every page load.
    
4.  Once a token from your backend checks out, click **Turn on** next to **Turn on signed sessions** in **Settings → Security → Customer identity**, then confirm with **Turn on**. Creating a key alone doesn't protect any fields or change how Gleap treats your users.
    

Sign the token on your backend, for example in Node.js:

```js
// npm install jsonwebtoken
const jwt = require("jsonwebtoken");

const token = jwt.sign(
  {
    userId: String(user.id),
    email: user.email,
    name: user.name,
    company: { id: user.companyId, name: user.companyName },
  },
  process.env.GLEAP_IDENTITY_SECRET,
  { algorithm: "HS256", keyid: process.env.GLEAP_IDENTITY_KEY_ID, expiresIn: "30m" }
);
```

[Sign the token on your backend](https://docs.gleap.ai/en/articles/709-customer-identity.md#sign-the-token-on-your-backend) lists every claim and shows PHP, Python, Ruby, C# and Go.

Then sign the user in from your app:

```js
await Gleap.setUserJwt(token);

// Optional: fetch a fresh token for renewal
Gleap.setUserJwtProvider(async () => {
  const response = await fetch("/api/gleap-token", { credentials: "include" });
  const { token } = await response.json();
  return token;
});
```

The token names the user and carries the contact data, so you don't need a separate `identify` call. `setUserJwt` and `setUserJwtProvider` need 19.5.0 or newer. `setUserJwtProvider` is optional: the SDK calls it for a fresh token when the session grant is about to run out or a token was refused as expired. From 19.5.2 the provider also restores the signed session after a page reload when the SDK holds no grant.

Calling `setUserJwt` again with a token for the same user and data sends nothing new: the SDK keeps the session grant and only signs in again when the claims change or the grant has run out. The session grant lasts as long as **Signed login lasts** (7 days by default); the token's `exp` (for example 30 minutes) only gates the sign-in.

From 19.7.0, when the grant runs out and no fresh token arrives (no `setUserJwt` on that page load, and the provider returns `null` or isn't set), the signed session ends. The user continues as a visitor without conversation history. Calling `setUserJwt` again signs them in and merges their visitor activity on this device into their contact. If the Messenger is open at that moment, the session ends once it's closed, at the latest an hour later. `Gleap.identify()` without a token isn't affected.

Call `Gleap.clearIdentity()` on logout and before you sign in another user. `Gleap.setSessionDuration(ms)` (or the `sessionDuration` option of `Gleap.initialize`) shortens the signed session; the project's maximum applies. A refused token is logged to the browser console; see [Refused tokens](https://docs.gleap.ai/en/articles/709-customer-identity.md#refused-tokens).

Moving from an older integration? See [Upgrade to identity tokens](https://docs.gleap.ai/en/articles/16752008-upgrade-to-identity-tokens.md).

### Identify without a token

`Gleap.identify()` without a token still works, but the contact is **unsigned**: anyone could claim that user ID. The widget then shows only conversations from the last 14 days, [protected fields](https://docs.gleap.ai/en/articles/709-customer-identity.md#protected-fields) keep their values, and Gleap refuses the call on platforms (web, iOS, Android) where you turned on [Require a signature](https://docs.gleap.ai/en/articles/709-customer-identity.md#require-a-signature).

```javascript
Gleap.identify("19283", {
  name: "Franz",
  email: "franz@gleap.ai",
  phone: "+1 (902) 123123",
  value: 199.95,
  plan: "Pro plan",
  // Optional: associate the user with a company.
  company: {
    id: "acme-inc",
    name: "ACME inc.",
  },
  // Passing the createdAt property allows you to set the correct createdAt date. (optional)
  createdAt: new Date(),
  customData: {
    key1: "awesome",
    key2: true,
  },
});
```

The optional `company` object associates the user with a company. Only `company.id` is required — it is your own identifier for the company. The optional `company.name` is a fallback label and never overwrites a name set authoritatively via the [server-side Companies API](https://docs.gleap.ai/en/articles/708-rest.md#companies).

> **Info:** The nested `company` object requires SDK version 16.3.0 or later. On earlier versions, pass the flat `companyId` and `companyName` properties instead — they remain fully supported.

> **Info:** Custom data takes strings, numbers, booleans, dates and arrays of strings or numbers (at most 100 keys per call). Only strings and numbers can be used in segment filters.

> **Info:** Gleap defaults to guest sessions when feedback items get reported without calling the identify method first. All feedback items of an existing guest session will be merged with the user session once you've identified it.

### Protected conversation files

To require authentication for new conversation attachments, turn it on in **Settings → Security → Conversation files**. [Authenticated conversation files](https://docs.gleap.ai/en/articles/710-authenticated-conversation-files.md) explains the setup, including the customer login URL and redirect. Only signed-in customers can open protected files, so call `Gleap.setUserJwt()` on every page load.

### Update contact properties

Use `Gleap.updateContact()` when contact details change during use, for example when a user subscribes to a new plan or changes their data. It updates the contact in real time. It can't change protected fields: put values you rely on into the token.

```javascript
Gleap.updateContact({
  value: 199.95,
  plan: "Pro plan",
});
```

The updateContact method takes in the same contact parameters as the identify method. Partial updates are possible.

```javascript
Gleap.updateContact({
  name: "Franz",
  email: "franz@gleap.ai",
  phone: "+1 (902) 123123",
  value: 199.95,
  plan: "Pro plan",
  company: {
    id: "acme-inc",
    name: "ACME inc.",
  },
  customData: {
    key1: "awesome",
    key2: true,
  },
});
```

### Removing data

Updates are applied incrementally. If you want to remove data, you can do so by adding a key value pair with `null` as value.

```javascript
Gleap.updateContact({
  keyToDelete: null,
});
```

### Clear the identity on logout

We recommend to clear the identity once the user logs out. Clearing the identity will automatically detach the current session and create a new guest session. It also drops the session grant on this device.

```js
Gleap.clearIdentity();
```

### Get the identification status of a user

With the following method, you can get the status of the current user identity.

```js
Gleap.isUserIdentified();
```

### Get the current user identity

With the following method, you can get the current user identity.

```js
Gleap.getIdentity();
```

It returns `{ gleapId, gleapHash, grant }` next to the user's fields. `grant` is `null` when the session has none. A help center or feedback portal on your own domain reads the sign-in automatically. Elsewhere, hand these values over and include `grant` only when it is set:

```js
const { gleapId, gleapHash, grant } = Gleap.getIdentity();
const params = new URLSearchParams({ gleapId, gleapHash });
if (grant) params.set('grant', grant);
```

See [Sign visitors in to your help center](https://help.gleap.ai/en/articles/16751994-sign-visitors-in-to-your-help-center).

## Related

- [JavaScript SDK setup](https://docs.gleap.ai/en/articles/393-javascript-sdk-setup.md)
- [JavaScript SDK: Code snippet](https://docs.gleap.ai/en/articles/394-javascript-sdk-code-snippet.md)
- [JavaScript SDK: Callbacks](https://docs.gleap.ai/en/articles/396-javascript-sdk-callbacks.md)
- [JavaScript SDK: AI chatbar](https://docs.gleap.ai/en/articles/397-javascript-sdk-ai-chatbar.md)
- [JavaScript SDK: Frontend tools](https://docs.gleap.ai/en/articles/398-javascript-sdk-frontend-tools.md)
