Socket
Socket
Sign inDemoInstall

apollo-link-opaque-refresh

Package Overview
Dependencies
37
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    apollo-link-opaque-refresh

Apollo Link for token refresh of JWT or Opaque tokens


Version published
Weekly downloads
179
decreased by-21.15%
Maintainers
1
Install size
17.8 kB
Created
Weekly downloads
 

Readme

Source

npm i apollo-link-opaque-refresh

Apollo Link to handle expired tokens (both JWT and opaque tokens) on auth-expired requests.

What it does

This link operates bi-directionally. When a query responds with an error, a handler is triggered to determine whether that response conforms to a "token expired" error.

If the request is deemed to be a token expiry, the query will be queued for re-attempt, and the token refresh handler will be started.

While the token is being refreshed, all requests attempted will be paused and queued before hitting terminating link. Once the token refresh is finished (either successful refresh or failure to refresh) the queue will be processed and all requests will be resumed.

What it doesn't do

This link does not check the token before sending queries. It operates on responses, therefore allowing a silent refresh mechanism for all types of tokens. It will pause outgoing queries if an active refresh is present. This is useful if your token expiry offset is unreliable on client or your tokens are using a proprietary format.

If you want to refresh and queue your queries prior to making a failing request, try newsiberian/apollo-link-token-refresh

Quick Start

tokenRefreshLink({
  refreshToken: async () => {
    try {
      await performSomeTokenRefresh();
    } catch (error) {
      userSignOut();
      throw error;
    }
  },
  shouldRefresh: ({ operation, result, networkError }) => {
    return networkError?.statusCode === 401;
  },
})

Configuration

The link takes a single options object with the following required properties:

propertydescription
refreshToken() => PromiseImplements your token refresh mechanism. Throw an error if unable to refresh. Successful resolve implies token was refreshed and is available to further links.
shouldRefresh({ operation, result, networkError }) => booleanUsing the provided values should determine whether the response warrants a token refresh.

FAQs

Last updated on 26 Oct 2020

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc