-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from frontapp/verified_user_example
Add verified user example
- Loading branch information
Showing
5 changed files
with
2,615 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
Oops, something went wrong.