Automatic (periodic) token refresh for the Nuxt Auth Module v5.
The current Auth Module only refreshes tokens before requests. This can be problematic when the user is staying on a page for a longer time without calling any apis or when the token livetime is very short. This plugin helps by refreshing near to expire tokens automatically in the background.
yarn add nuxt-auth-auto-refresh
npm i nuxt-auth-auto-refresh
Add the plugin to the plugins
array of the Auth Module in your nuxt.config.js
.
auth: {
// Other config...
plugins: [{ src: "node_modules/nuxt-auth-auto-refresh/dist/index.js", ssr: false }]
}
This is not the normal plugins block. Nuxt Auth has it's own to make it possible to build on it.
The plugin works by searching periodically (every 500ms) for soon to expire tokens.
A token is considered "soon to expire" when it has exceeded 75% of it's lifetime (the difference between iat
and exp
) but is not completely expired yet.
If such a token is found, the refreshTokens()
function of the Auth Module is called.