Refresh Access Token
Overview
This plugin is designed to manage the expiration and refreshing of access tokens for an eCommerce API. It checks the validity of the access token stored in a cookie and refreshes it when necessary. The plugin operates as follows:
- Checks the access token's validity based on its expiration time.
- If the token is expired or invalid, it triggers a refresh process.
- Periodically checks and refreshes the token at set intervals.
Setup
1. Access Token Validation
Upon initialization, the plugin checks whether the access token exists in the cookies and whether it is valid. The plugin uses the EcomService.isTokenValid method to verify the token against its expiration time. If the token is invalid or expired, it triggers the EcomService.refreshToken method to obtain a new token.
const accessTokenData = useCookie("ecomApiAccessToken");
if (
!accessTokenData.value ||
!accessTokenData.value.accessToken ||
!accessTokenData.value.expiresTime ||
!EcomService.isTokenValid(
accessTokenData.value.accessToken,
accessTokenData.value.expiresTime,
EcomService.accessTokenExpiryBuffer
)
) {
await EcomService.refreshToken();
}
2. Token Refresh Interval
To ensure that the token is always valid, the plugin sets up a periodic interval to check the token's validity. This interval checks the token at regular intervals, defined by EcomService.accessTokenPollInterval, and attempts to refresh it if it is about to expire.
const interval_time = EcomService.accessTokenPollInterval * 1000;
rootStore.access_token_interval_id = setInterval(async () => {
try {
const accessTokenData = useCookie("ecomApiAccessToken");
if (
!accessTokenData.value ||
!accessTokenData.value.accessToken ||
!accessTokenData.value.expiresTime ||
!EcomService.isTokenValid(
accessTokenData.value.accessToken,
accessTokenData.value.expiresTime,
EcomService.accessTokenExpiryBuffer
)
) {
await EcomService.refreshToken();
}
} catch (error) {
console.error("Failed to refresh token:", error);
}
}, interval_time);
3. Token Expiry Buffer
A buffer (EcomService.accessTokenExpiryBuffer) is used to ensure that the token is refreshed before it fully expires. The interval checks occur just before the expiration time to ensure a seamless user experience.
4. Token Refresh Process
The EcomService.refreshToken method is responsible for refreshing the access token. This method should be implemented to make an API request to the eCommerce service to obtain a new access token and update the cookie.
await EcomService.refreshToken();
5. Cleanup
This plugin does not require explicit cleanup, as the refresh interval is automatically managed by the root store.
Example of Usage
You don't need to do anything additional to use this plugin. Simply include it in your Nuxt app as part of your plugin configuration, and it will automatically handle the access token refreshing.
Notes
- Token Expiry Buffer: The buffer is set to ensure that the token is refreshed before it expires.
- Custom Interval: The polling interval for refreshing the token is set using
EcomService.accessTokenPollInterval, which is typically set to a few minutes.