Performance: Refresh token pattern
This pattern affects the responsiveness and load of authentication requests, impacting user interaction speed and server load.
async function refreshToken(oldToken) { // Use async non-blocking verification const user = await verifyTokenAsync(oldToken); const newToken = generateToken(user); return newToken; }
async function refreshToken(oldToken) { // Synchronously block to verify and generate new token const user = verifyTokenSync(oldToken); const newToken = generateToken(user); return newToken; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous token verification | 0 | 0 | 0 | [X] Bad |
| Asynchronous token verification | 0 | 0 | 0 | [OK] Good |
| Memory bloat from indefinite token storage | 0 | 0 | 0 | [X] Bad |
| Token storage with expiration and cleanup | 0 | 0 | 0 | [OK] Good |
refresh token in a NestJS authentication system?jwtService.verify() checks token validity and signature.jwtService.verify(refreshToken) to confirm it is valid and not expired.async refreshToken(token: string) {
try {
const payload = this.jwtService.verify(token);
const newAccessToken = this.jwtService.sign({ userId: payload.sub });
return { accessToken: newAccessToken };
} catch (e) {
throw new UnauthorizedException('Invalid refresh token');
}
}async refreshToken(token: string) {
const payload = this.jwtService.verify(token);
const newAccessToken = this.jwtService.sign({ userId: payload.userId });
return { accessToken: newAccessToken };
}Pass the test and this pattern moves unknown