Under the Hood
Tokens are required to make an API secure from any attacks in future and to judge the authenticity of a request. It’s always a good approach to add the token in headers or body parts of all sensitive APIs. One of the best tokens to work with is JWT — JSON Web Token is a new secured way to make an API request to your server. JWT has 3 parts Payload, Signature and Header.
- Header — Defines the type of token and algorithm users under the hood.
- Payload — Payload consists of data transferred while making an API request.
- Signature — The signed part of JWT used to decode/detect the authenticity of the token.
The main agenda behind the article is to avoid storing these tokens in redux store or local storage and Following is the reasons why we shouldn’t do that: 1. Storing tokens in redux gets erased when the user reloads the application (Although there a way around in redux to avoid this issue)2. Redux storage can be easily cleared/deleted by the user.3. Redux store can’t be accessed on the server-side(Will cover this later on).4. LocalStorage store tokens for a longer time than expected.5. LocalStorage is prone to attacks hence the data stored is not secured either.
What is a perfect way to store tokens and manage user sessions?I always preferred to store the tokens in a cookie and set the expiration of the token. Following are the benefits of saving tokens in a cookie: 1. Cookie token can be accessed on the server and client-side easily.2. Cookie token will automatically get expired after a maximum of 14 days.3. Cookie data remain consistent even when the user reloads the application(No need to add extra layer to make data persisted after reload).4. Cookie data remain persisted among all tabs of the browser which makes it easy to manage sessions in multiple tabs all at the same time.
Setting a token in a cookie
Cookie has its methods and Browser gives us a method to add the cookie, delete the cookie and edit the cookie. Adding a cookie method takes 3 arguments cookie name, cookie value and cookie expiration time(in days). The third argument helps to expire the token after every “x” day (x = expiration days you set as the third argument of the method) and once the token is expired it gets deleted from the Browser memory.

We have created the functions to set the cookie, fetch the cookie and delete the cookie inside a folder named “utils”. In this way, we can reuse these methods on multiple components to retrieve the cookie, set the cookie and delete the cookie.
Managing user sessions
We always saved the authentication token in the cookie with the name “authToken”. We will use the code repository once we install firebase authentication.
Here is the code: https://github.com/shreyvijayvargiya/iHateReadingLogs/tree/main/TechLogs/FirebaseAuthentication.
Approach
- Add authentication methods of API, we are using firebase and I have added login with an email and the Google Firebase method.
- Once the login and sign up methods are successfully added the firebase method returns the user “uid” as “authToken”, we will treat that as an authentication token and store that token in a cookie.
- Now on the client or server-side depends on your need, we will fetch the authToken from the cookie.
- Check the authenticity of the “authToken”, you can use the firebase methods itself to check the user “uid” value is correct or not.
- Since we have set expiration of 14 days the token get erased after every 14 days.
- If token if not present then logged out the user from all secured or protected pages on your website. For example, the user can only use the dashboard when the token is present in the cookie and when that token is a correct token.

Add the user “uid” and “email” in the cookie using the “setCookie” method from the Firebase login API. After the user is logged in we will throw the user to a protected page like the dashboard page in our case. In addition, we need to add some checks on the dashboard page also, whether “authToken” exists in the cookie and whether that token is correct or not.
Once this is done our main target is to fetch this cookie on the server-side for the dashboard page. In our case, we will check whether the “authToken” exists in a cookie or not, if not, then before redirecting to the dashboard page we will throw the user to the login page. We will add the same check to our login page. Once the user is logged in he/she can’t access the login page and if he/she tries to do the same, we will redirect them to the dashboard page.
We will use the next-cookie npm package to access the cookie and we will add this functionality inside the “getServerSideProps” of the login and dashboard page.
The “getServerSideProps” can only be used inside the route page of the “pages” directory. Read more here https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering

Conclusion
We have to write a maximum of 30/40 lines of code and install one npm package for this setup. I found this method as the easiest and powerful in comparison to storing tokens in redux store or LocalStorage. In addition, you can add more powerful functionality inside the getServerSideProps. For example, fetching data from API using the same “authToken” in API headers followed by passing the data to the dashboard component to display the data on the page, this is called pre-fetching the data for pages.
Hope you have learned something from the article, Until Next time, Have a good day, people.
Here is the code repository
https://github.com/shreyvijayvargiya/iHateReadingLogs/tree/main/TechLogs/TokenInFrontend
Shrey
