I saved the token from createdirectline first time it was initialized to the server. But whenever I get the token from the server and used it, it will not use that token instead it will create a new one.
React.useEffect(() => {
const temp = async () => {
if (Object.entries(directline).length === 0) {
if(await props.chatBotData!.tokenFromDb){
setDirectline(createDirectLine({ token: props.chatBotData.tokenFromDb.toString() }));
}else {
setDirectline(createDirectLine({ token: DIRECTLINE_SECRET }));
}
}
};
temp();
}, [directline,props.chatBotData]);
const [directline, setDirectline]: any = useState({});
You want to save the Direct Line secret
server-side so it is inaccessible, not the token. The secret
is used when making a call to /directline/tokens/generate
or /directline/conversations
in order to retrieve a token (and conversationId
, if the latter endpoint). (Reference Connect a bot to Direct Line and Generate a Direct Line token.)
In short, this is the flow your site should follow:
https://.../gettoken
./directline/tokens/generate
passing the secret
which then returns the token
.createDirectLine()
.It's important to note that tokens are only good for 30 mins, at the time of this writing, before they expire. If you are expecting a conversation to last longer than 30 mins, then you will need to perform a similar process to exchange the token for a refreshed token beforehand.
Hope of help!