Have You Ever Wondered How Websites Keep You Logged In Securely?
Table of contents
In today's digital world, security is paramount. Have you ever wondered how websites keep you logged in without asking for your password repeatedly? The secret lies in the clever use of access tokens and refresh tokens. Let's explore how these digital keys work.
The Library Analogy
Imagine you're visiting a grand library with many restricted sections. To enter these sections, you need a special pass, much like how you need permissions to access certain parts of a website. Here’s where access tokens and refresh tokens come into play.
Access Token: Picture the access token as a temporary pass to the library’s restricted sections. It's only valid for a short time, ensuring that if it falls into the wrong hands, it won't be useful for long.
Refresh Token (Session Token): On the other hand, the refresh token is like your annual library membership card. It lasts much longer and allows you to renew your temporary pass without having to reapply for membership.
How Access Tokens and Refresh Tokens Work
Step 1: Logging In
When you first log in to a website with your username and password, the server checks your credentials. If everything matches up, it grants you an access token and a refresh token. Think of it as getting your temporary pass and membership card.
You: Login with username and password.
Server: Here is your access token (valid for 15 minutes) and refresh token (valid for 7 days).
Step 2: Using the Access Token
With your access token in hand, you can now explore the restricted sections of the website. Each time you want to access a resource, you present your access token.
You: I want to see my profile (send access token).
Server: Access token is valid. Here is your profile.
Step 3: Expiration of the Access Token
However, after a short while, your access token expires. This is a security measure to ensure that even if someone gets hold of your token, they can't use it indefinitely.
You: I want to see my profile (send access token).
Server: Access token expired. You cannot see your profile.
Step 4: Renewing the Access Token
Instead of logging in again, you use your refresh token to get a new access token. This is like showing your membership card to get a new temporary pass.
You: My access token expired. Here is my refresh token. Can I get a new access token?
Server: Refresh token is valid. Here is your new access token (valid for 15 minutes).
Step 5: Continuing with the New Access Token
With the new access token, you can continue accessing the restricted sections without a hitch.
You: I want to see my profile (send new access token).
Server: New access token is valid. Here is your profile.
This seamless process ensures that you can stay logged in and secure without the hassle of re-entering your password repeatedly.
By using access tokens and refresh tokens, websites maintain a balance between convenience and security. The short lifespan of access tokens minimizes the risk if they are compromised, while refresh tokens allow users to stay logged in comfortably. So, next time you navigate a website without constantly logging in, you'll know the magic behind it.
For a deeper dive into understanding access and refresh tokens, you might find Hitesh Choudhary's explanation incredibly helpful. Known for his Chai aur Code style, he simplifies tech concepts with ease. Check out his video here to get a clear and straightforward explanation.
Subscribe to my newsletter
Read articles from Shanu Raw directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Shanu Raw
Shanu Raw
I am developer from India