Skip to content

Add support for auth_tokens in boot and update methods #820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changeset/add-auth-tokens-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
"react-use-intercom": minor
---

Add support for auth_tokens in boot and update methods

Users can now pass authentication tokens to Intercom for secure data operations. The `authTokens` property accepts an object with any string key-value pairs.

Example usage:
```js
boot({
email: '[email protected]',
userId: '9876',
authTokens: {
security_token: 'abc...' // JWT
}
})
```
37 changes: 37 additions & 0 deletions examples/auth-tokens-example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import { IntercomProvider, useIntercom } from 'react-use-intercom';

function MyApp() {
const { boot } = useIntercom();

const handleLogin = () => {
// After successful login, boot Intercom with auth tokens
boot({
email: '[email protected]',
createdAt: 1234567890,
name: 'John Doe',
userId: '9876',
authTokens: {
security_token: 'abc...', // Your JWT token
// You can add any other tokens as key-value pairs
api_token: 'xyz...',
custom_token: '123...'
}
});
};

return (
<div>
<h1>Intercom Auth Tokens Example</h1>
<button onClick={handleLogin}>Login and Boot Intercom</button>
</div>
);
}

export default function App() {
return (
<IntercomProvider appId="your-app-id">
<MyApp />
</IntercomProvider>
);
}
17 changes: 17 additions & 0 deletions packages/react-use-intercom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,23 @@ All the Intercom default attributes/props are camel cased (`appId` instead of `a
})
```

#### Authentication tokens
For secure data operations, you can pass authentication tokens to Intercom using the `authTokens` property. This accepts an object with any string key-value pairs.

```ts
const { boot } = useIntercom();

boot({
email: '[email protected]',
userId: '9876',
authTokens: {
security_token: 'abc...', // JWT token
api_token: 'xyz...',
// Any other tokens as key-value pairs
}
})
```

## Playground
Small playground to showcase the functionalities of `react-use-intercom`.

Expand Down
1 change: 1 addition & 0 deletions packages/react-use-intercom/src/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const mapDataAttributesToRawDataAttributes = (
mapDataAttributesCompanyToRawDataAttributesCompany,
),
intercom_user_jwt: attributes.intercomUserJwt,
auth_tokens: attributes.authTokens,
...attributes.customAttributes,
});

Expand Down
19 changes: 18 additions & 1 deletion packages/react-use-intercom/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export type DataAttributesAvatar = {
imageUrl?: string;
};

export type AuthTokens = Record<string, string>;

export type RawDataAttributes = {
email?: string;
user_id?: string;
Expand All @@ -137,6 +139,7 @@ export type RawDataAttributes = {
companies?: RawDataAttributesCompany[];
intercom_user_jwt?: string;
customAttributes?: Record<string, any>;
auth_tokens?: AuthTokens;
};

export type DataAttributes = {
Expand Down Expand Up @@ -236,9 +239,23 @@ export type DataAttributes = {
* ```
*
* @see {@link https://www.intercom.com/help/en/articles/179-send-custom-user-attributes-to-intercom}
* @remarks The key is the attribute name. The value is a placeholder for the data youll track
* @remarks The key is the attribute name. The value is a placeholder for the data you'll track
*/
customAttributes?: Record<string, any>;
/**
* Authentication tokens for secure data operations
* Can contain any key-value pairs where both key and value are strings
*
* @example
* ```
* authTokens: {
* security_token: 'abc...' // JWT
* }
* ```
*
* @see {@link https://www.intercom.com/help/en/articles/6615543-setting-up-data-connectors-authentication#h_5343ec2d2c}
*/
authTokens?: AuthTokens;
};

export type IntercomMethod =
Expand Down
75 changes: 75 additions & 0 deletions packages/react-use-intercom/test/authTokens.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { act, renderHook } from '@testing-library/react';
import * as React from 'react';

import { IntercomProvider, useIntercom } from '../src';

describe('auth_tokens', () => {
it('should pass auth_tokens during boot', () => {
const appId = 'app123';
const mockIntercom = jest.fn();
(window as any).Intercom = mockIntercom;
(window as any).intercomSettings = undefined;

const wrapper = ({ children }: { children: React.ReactNode }) => (
<IntercomProvider appId={appId}>{children}</IntercomProvider>
);

const { result } = renderHook(() => useIntercom(), { wrapper });

act(() => {
result.current.boot({
email: '[email protected]',
createdAt: 1234567890,
name: 'John Doe',
userId: '9876',
authTokens: {
security_token: 'abc123',
another_token: 'xyz789',
},
});
});

expect(mockIntercom).toHaveBeenCalledWith('boot', {
app_id: appId,
email: '[email protected]',
created_at: 1234567890,
name: 'John Doe',
user_id: '9876',
auth_tokens: {
security_token: 'abc123',
another_token: 'xyz789',
},
});
});

it('should pass auth_tokens during update', () => {
const appId = 'app123';
const mockIntercom = jest.fn();
(window as any).Intercom = mockIntercom;
(window as any).intercomSettings = { app_id: appId };

const wrapper = ({ children }: { children: React.ReactNode }) => (
<IntercomProvider appId={appId} autoBoot>{children}</IntercomProvider>
);

const { result } = renderHook(() => useIntercom(), { wrapper });

act(() => {
result.current.update({
authTokens: {
security_token: 'updated_token',
},
});
});

// Find the update call (not the boot or event handler calls)
const updateCalls = mockIntercom.mock.calls.filter(call => call[0] === 'update');
const lastUpdateCall = updateCalls[updateCalls.length - 1];
expect(lastUpdateCall).toBeDefined();
expect(lastUpdateCall[1]).toMatchObject({
auth_tokens: {
security_token: 'updated_token',
},
});
});
});
Loading