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

@wavesenterprise/api-token-refresher

Package Overview
Dependencies
Maintainers
11
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wavesenterprise/api-token-refresher

Wavesenterprise JWT refresh library for both Node.js and browser.

  • 3.2.0
  • npm
  • Socket score

Version published
Weekly downloads
12
Maintainers
11
Weekly downloads
 
Created
Source

Wavesenterprise JWT refresh library for both Node.js and browser.

Installation

npm install @wavesenterprise/api-token-refresher --save
npm install axios@0.21.4

Example of usage

config

type TokenPair = {
  access_token: string,
  refresh_token: string,
}


export type TokenRefresherConfig = {
  authorization: TokenPair, // inital auth pair
  refreshCallback(refreshToken: string): Promise<TokenPair>, // called to get new TokenPair
  axiosRequestConfig?: AxiosRequestConfig, // optional, custom AxiosRequestConfig
  onChangeAccessToken?(token: string): void, // optional, called after success token refresh
  onRefreshFailed?(err: any): void, // optional, default console.log(err), called in case refresh error
  refreshBeforeExpire?: number, // optional, default 30000, refresh will start if expiration - refreshBeforeExpire < Date.now()
  minTimeBeforeExpireToSendReq?: number, // optional, default 1000, if expiration - minTimeBeforeExpireToSendReq < Date.now() token will be updted synchronously before req
  maxAttemptsToRefreshToken?: number, // optional, default 5
  sleepAfterError?: number, // optional default 1000
}

Usage

Full example can be found in examples/ folder

import { ApiTokenRefresher } from '@wavesenterprise/api-token-refresher'
import axios from 'axios'

const refreshCallback = async (token: string) => {
  return Promise.reject('just test')
  try {
    const { data } = await axios.post(`${AUTH_SERVICE_ADDRESS}/v1/auth/refresh`, { token })
    console.log('token refreshed')
    return data
  } catch (e) {
    console.log('refresh failed relogin')
    return getTokens()
  }
}

const apiTokenRefresher = new ApiTokenRefresher({
    authorization: tokens,
    refreshCallback,
 })
const { axios } = apiTokenRefresher.init()

FAQs

Package last updated on 18 Oct 2021

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