0
0
NestJSframework~10 mins

Refresh token pattern in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Refresh token pattern
User logs in
Server issues Access Token + Refresh Token
User uses Access Token to access resources
Access Token expires?
NoContinue using Access Token
Yes
User sends Refresh Token to server
Server verifies Refresh Token
Refresh Token valid?
NoReject request, ask login
Yes
Server issues new Access Token (and optionally new Refresh Token)
User continues with new Access Token
This flow shows how a user logs in, gets tokens, uses access token until it expires, then uses refresh token to get a new access token without logging in again.
Execution Sample
NestJS
async refreshToken(oldRefreshToken: string) {
  const user = await this.jwtService.verifyAsync(oldRefreshToken);
  if (!user) throw new UnauthorizedException();
  return this.jwtService.sign({ id: user.id });
}
This function verifies the refresh token and returns a new access token if valid.
Execution Table
StepActionInputCheck/ConditionResult/Output
1User logs inusername/passwordCredentials valid?Issue Access Token + Refresh Token
2User accesses resourceAccess TokenAccess Token valid?Allow access
3Access Token expiresAccess Token expiredExpired?User must refresh token
4User sends Refresh TokenRefresh TokenVerify Refresh TokenValid or Invalid
5Refresh Token validValid Refresh TokenIs token valid?Issue new Access Token
6Refresh Token invalidInvalid Refresh TokenIs token valid?Reject request, ask login
💡 Process stops when refresh token is invalid or user logs out.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 5Final
accessTokennullissuedexpirednew token issuedvalid
refreshTokennullissuedsent by userverifiedvalid or invalid
usernullauthenticatedextracted from tokenconfirmedauthenticated or rejected
Key Moments - 3 Insights
Why do we need both access and refresh tokens?
Access tokens are short-lived for security; refresh tokens allow getting new access tokens without logging in again, as shown in steps 3 to 5 in the execution_table.
What happens if the refresh token is invalid or expired?
As seen in step 6, the server rejects the request and asks the user to log in again to get new tokens.
Can the refresh token be used to access resources directly?
No, refresh tokens are only for getting new access tokens, not for accessing resources directly, as shown by the separate steps for access token usage and refresh token verification.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the server verify the refresh token?
AStep 2
BStep 1
CStep 4
DStep 6
💡 Hint
Check the 'Action' and 'Check/Condition' columns in step 4.
According to variable_tracker, what is the state of accessToken after step 5?
Anew token issued
Bnull
Cexpired
Dinvalid
💡 Hint
Look at the 'accessToken' row under 'After Step 5' column.
If the refresh token is invalid, what does the server do according to the execution_table?
AAllow access to resources
BReject request and ask login
CIssue new access token
DIgnore and continue
💡 Hint
See step 6 in the execution_table under 'Result/Output'.
Concept Snapshot
Refresh Token Pattern in NestJS:
- User logs in, gets Access + Refresh Tokens
- Access Token used for resource access
- When Access Token expires, send Refresh Token
- Server verifies Refresh Token
- If valid, server issues new Access Token
- If invalid, user must log in again
- Keeps user logged in securely without frequent logins
Full Transcript
The refresh token pattern helps keep users logged in securely. When a user logs in, the server gives two tokens: an access token and a refresh token. The access token is used to access protected resources but expires quickly for safety. When it expires, the user sends the refresh token to get a new access token without logging in again. The server checks if the refresh token is valid. If yes, it sends a new access token. If not, the user must log in again. This process balances security and convenience.