Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@redon2inc/strapi-plugin-refresh-token

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@redon2inc/strapi-plugin-refresh-token

Plugin for Strapi 5 to provide refresh token functionality.

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12
decreased by-91.18%
Maintainers
0
Weekly downloads
 
Created
Source

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

{
  // .. rest of code
  "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:


  // ..other plugins
  'users-permissions': {
        config: {
          jwt: {
            /* the following  parameter will be used to generate:
             - regular tokens with username and password
             - refreshed tokens when using the refreshToken API
            */
            expiresIn: '2h', // This value should be lower than the refreshTokenExpiresIn below.
          },
        },
    },
  'refresh-token': {
    config: {
      refreshTokenExpiresIn: '30d', // this value should be higher than the jwt.expiresIn
      requestRefreshOnAll: false, // automatically send a refresh token in all login requests.
      refreshTokenSecret: env('REFRESH_JWT_SECRET') || 'SomethingSecret',
      cookieResponse: false // if set to true, the refresh token will be sent in a cookie
    },
  }

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": { /* user object */ },
  "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.

FAQs

Package last updated on 04 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc