1. The first step is to install the packages by using the following command in your project’s root-
npm install universal-cookie --save
2. Import the Cookies Class, where-ever you want to create a Cookie object in your repository.
import Cookies from 'universal-cookie';
Create a Cookie object at the same time in the file you want to use it,
const cookies = new Cookies();
3. We make use of the set function of the package first, where we try to set the cookie while the User is trying to login to the account.
Note - The cookie value can be set to any value one wants, however, here I am setting it to the access token which is generated by the server so that I can access it throughout the application.
4. To access the value set to the cookie, we make use of the get function. To check the logged in state of the User we check if get method is returning a null value or an undefined value, this helps in maintaining the User behaviour at every point in the application.
// Handle User behaviours do not send chat queries with access token if the cookie is null
url = BASE_URL+'/susi/chat.json?q='+
// Send the messages with User’s access token
url = BASE_URL+'/susi/chat.json?q='
5. To delete the cookies, we make use of the remove function, which deletes that cookie. This function is called while logging the user out of the application.
Here’s the full code in the repository. Feel free to contribute:https://github.com/fossasia/chat.susi.ai