Strapi5 Refresh Token plugin
Strapi Plugin that extends the local authorization functionality to provide Refresh tokens.
⚠️ Compatibility with Strapi versions
- This plugin relies on Strapi5 new
documentId
. It will not work with earlier versions! - Works with
local
provider only.
⚙️ Installation
To install the Strapi Refresh Token Plugin, simply run one of the following command:
npm install @redon2inc/strapi-plugin-refresh-token
yarn add @redon2inc/strapi-plugin-refresh-token
Config
You will need to set the following environment variables:
PRODUCTION_URL=value # used for cookie security if enabled
REFRESH_JWT_SECRET=string
This component relies on extending the user-permissions
types. Extend it by adding the following to ./src/extensions/user-permissions/content-types/user/schema.json
{
"refresh_tokens": {
"type": "relation",
"relation": "oneToMany",
"target": "plugin::refresh-token.token",
"mappedBy": "user",
"private": true,
"configurable": false
}
}
Modify your plugins file config/plugin.ts
to have the following:
'users-permissions': {
config: {
jwt: {
expiresIn: '2h',
},
},
},
'refresh-token': {
config: {
refreshTokenExpiresIn: '30d',
requestRefreshOnAll: false,
refreshTokenSecret: env('REFRESH_JWT_SECRET') || 'SomethingSecret',
cookieResponse: false
},
}
API Usage:
when calling POST
:/api/auth/local
include the requestRefresh
parameter:
{
"identifier":"username",
"password":"VerySecurePassword",
"requestRefresh": true
}
The API will respond with the following:
{
"jwt":"token...",
"user": { },
"refreshToken": "RefreshToken..."
}
to request a new access token use the following:
POST
:/api/auth/local/refresh
with the following payload:
{
"refreshToken": "RefreshToken...",
}
if the Refresh token is valid, the API will return
{
"jwt": "NewAccessToken..",
}
TODO:
- Currently the tokens do not get removed from the DB on usage. They are cleaned when a new token is requested and the old ones have expired.
- Expose API so user can clear all sessions on their own.