Access Tokens and Refresh Tokens
Access Token
The Access Token is a small piece of data that contains user identity information, such as email, name, and user ID (_id). This token typically has a short lifespan, ranging from 5 to 15 minutes. Due to its comprehensive user information, it’s important to change the Access Token periodically. This ensures that any sensitive information is refreshed regularly.
Refresh Token
In contrast, the Refresh Token is specifically designed to be used when the Access Token expires. It only includes the user ID, which is essential for validating the user. When the Access Token expires, the Refresh Token can be utilized to generate a new Access Token. This process occurs without requiring the user to log in again, enhancing the user experience.
Token Management
The creation of a new Access Token is triggered automatically upon the expiration of the previous one. Since the Access Token contains all the necessary user information, it effectively reduces the number of requests made to the database. This not only improves performance but also streamlines the authentication process.
How Tokens Work
The main principle of the Access Token is to hold encrypted user data, allowing applications to cache information in requests as cookies. However, it is important to note that the Access Token is short-lived. This design choice enhances security while still providing quick access to user data.
On the other hand, the Refresh Token allows applications to obtain a new Access Token without requiring the user to log in again. This is particularly beneficial for maintaining a smooth user experience.
Storage
Regarding storage, the Access Token is typically stored in memory or in httpOnly cookies for quick access. This method ensures that the token is easily retrievable for subsequent requests. In contrast, the Refresh Token is stored in httpOnly and Secure cookies for web applications. For mobile applications, it is kept in secure storage, ensuring the highest level of security.
For better understanding in conclusion
Access Token and Refresh Token Both are stored in the user's cookies or header.
The Access Token contains user identity information (like email, name, and _id) and has a short lifespan (typically 5-15 minutes). The Refresh Token is used to generate a new Access Token when it expires. The Access Token should be changed periodically due to its comprehensive user information.
The Refresh Token only includes the user ID, which is used to validate the user by comparing it with the stored version in the database. If the user is authenticated, a new Access Token is created and sent.
Access Token creation is triggered upon expiration. Since the Access Token contains all necessary user information, it reduces database requests.
The main principle of the Access Token is to hold encrypted user data, providing cached information in requests as cookies. However, it is short-lived.
The Refresh Token allows for obtaining a new Access Token without requiring the user to log in again.
The Access Token is stored in memory or httpOnly cookies for quick access. The Refresh Token is stored in httpOnly and Secure cookies for web applications and in secure storage for mobile applications.
Subscribe to my newsletter
Read articles from Atharv Sankpal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Atharv Sankpal
Atharv Sankpal
Welcome to my blog! My name is Atharv and I am a developer with a passion for web development with strong understanding of data structures and algorithms (DSA). Recently, I have also been exploring the exciting world of app development and would likely also explore artificial intelligence/machine learning (AI/ML). I am constantly learning and experimenting with new technologies, and I enjoy sharing my knowledge and experience through this blog. You can expect to find a variety of posts on topics such as web development best practices, DSA tips and tricks, and my journey as I dive deeper into the world of app development and hopefully AI/ML. Thank you for visiting and I hope you find my blog informative and engaging. I would love to hear from you, so please feel free to leave comments or reach out to me with any questions or feedback.