Skip to content
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

Add verified user example #13

Merged
merged 4 commits into from
Aug 26, 2024
Merged
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ After the last command, the example application can be accessed at http://localh

There are four helpers provided in this repository, and each of them is used in the [examples](https://github.com/frontapp/front-chat-sdk/tree/main/examples) directory.

Additionally, there is an example of how to embed a Front Chat widget [directly in a page](https://github.com/frontapp/front-chat-sdk/tree/main/examples/react-embed-front-chat). This example uses React, but the general principle should work for any application.
We have also provided additional examples for common use cases. These examples use React, but the general principles should work for any application.

- How to embed a Front Chat widget [directly in a page](https://github.com/frontapp/front-chat-sdk/tree/main/examples/react-embed-front-chat).
- How to initialize a Front Chat widget [with a verified user](https://github.com/frontapp/front-chat-sdk/tree/main/examples/react-verified-user).

#### Quick-start Helpers

Expand Down
60 changes: 60 additions & 0 deletions examples/react-verified-user/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {createHmac} from 'crypto';
import {useEffect, useState} from 'react';

import {useFrontChatBoot} from '../../lib/hooks/use-front-chat-boot';

/*
* Constants.
*/

const chatId = '<CHAT_ID_REQUIRED>';

// Usually this will be an email for a verified user.
const userId = '<USER_ID_REQUIRED>';

// This value is used to generate the user hash, and can be found in the Front channel settings.
const identitySecret = '<IDENTITY_SECRET_REQUIRED>';

/*
* User Hash.
*/

// This is a hash generated from the userId using the identity secret.
// Follow the instructions here to generate the user hash: https://dev.frontapp.com/docs/identify-users#computing-the-user-hash
// NOTE: This should only ever be generated server-side, and this is just an example. Do not generate this value client-side in your application.
const hmac = createHmac('sha256', identitySecret);
const userHash = hmac.update(userId).digest('hex');

/*
* Component.
*/

function App() {
const {frontChat, initialize, isInitialized} = useFrontChatBoot(document.body);

const [isWindowVisible, setIsWindowVisible] = useState(false);

// Example of using useFrontChat to initialize the chat widget when a component mounts.
useEffect(() => {
if (!initialize || isInitialized) {
return;
}

// When initializing for a verified user, you must provide the `userId` and `userHash` options.
initialize({chatId, userId, userHash});
}, [isInitialized, initialize, userHash]);

// NOTE: You can also start the widget with the chat window visible by providing the `shouldShowWindow`
// option to the 'init' command. The below effect is just an example of waiting for the chat widget to
// be initialized, before performing another action.
useEffect(() => {
if (!frontChat || !isInitialized || isWindowVisible) {
return;
}

frontChat('show');
setIsWindowVisible;
}, [frontChat, isInitialized, isWindowVisible]);

return <div>isInitialized: {isInitialized ? 'True' : 'False'}</div>;
}
Loading
Loading