Skip to content

Commit 2b0c93f

Browse files
authored
Merge pull request #13 from frontapp/verified_user_example
Add verified user example
2 parents d0ab525 + 8ef0e8c commit 2b0c93f

File tree

5 files changed

+2615
-49
lines changed

5 files changed

+2615
-49
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ After the last command, the example application can be accessed at http://localh
9898

9999
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.
100100

101-
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.
101+
We have also provided additional examples for common use cases. These examples use React, but the general principles should work for any application.
102+
103+
- 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).
104+
- How to initialize a Front Chat widget [with a verified user](https://github.com/frontapp/front-chat-sdk/tree/main/examples/react-verified-user).
102105

103106
#### Quick-start Helpers
104107

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {createHmac} from 'crypto';
2+
import {useEffect, useState} from 'react';
3+
4+
import {useFrontChatBoot} from '../../lib/hooks/use-front-chat-boot';
5+
6+
/*
7+
* Constants.
8+
*/
9+
10+
const chatId = '<CHAT_ID_REQUIRED>';
11+
12+
// Usually this will be an email for a verified user.
13+
const userId = '<USER_ID_REQUIRED>';
14+
15+
// This value is used to generate the user hash, and can be found in the Front channel settings.
16+
const identitySecret = '<IDENTITY_SECRET_REQUIRED>';
17+
18+
/*
19+
* User Hash.
20+
*/
21+
22+
// This is a hash generated from the userId using the identity secret.
23+
// Follow the instructions here to generate the user hash: https://dev.frontapp.com/docs/identify-users#computing-the-user-hash
24+
// 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.
25+
const hmac = createHmac('sha256', identitySecret);
26+
const userHash = hmac.update(userId).digest('hex');
27+
28+
/*
29+
* Component.
30+
*/
31+
32+
function App() {
33+
const {frontChat, initialize, isInitialized} = useFrontChatBoot(document.body);
34+
35+
const [isWindowVisible, setIsWindowVisible] = useState(false);
36+
37+
// Example of using useFrontChat to initialize the chat widget when a component mounts.
38+
useEffect(() => {
39+
if (!initialize || isInitialized) {
40+
return;
41+
}
42+
43+
// When initializing for a verified user, you must provide the `userId` and `userHash` options.
44+
initialize({chatId, userId, userHash});
45+
}, [isInitialized, initialize, userHash]);
46+
47+
// NOTE: You can also start the widget with the chat window visible by providing the `shouldShowWindow`
48+
// option to the 'init' command. The below effect is just an example of waiting for the chat widget to
49+
// be initialized, before performing another action.
50+
useEffect(() => {
51+
if (!frontChat || !isInitialized || isWindowVisible) {
52+
return;
53+
}
54+
55+
frontChat('show');
56+
setIsWindowVisible;
57+
}, [frontChat, isInitialized, isWindowVisible]);
58+
59+
return <div>isInitialized: {isInitialized ? 'True' : 'False'}</div>;
60+
}

0 commit comments

Comments
 (0)