npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

axios-token-handler

v1.2.7

Published

Automatically refreshes access tokens for axios requests

Downloads

72

Readme

Axios Token 🛡

Bother with handling jwt web tokens in your React project? Wanna a "magick stick" which does everything by its own? So, you are in right package!

Please welcome to axios-token - fastest way to handle web tokens.

Here are all steps you need to do:

Step 1

Create an instance of Axios and pass it to axiosToken as shown down below:

import { axiosToken } from "axios-token-handler";

const axios = Axios.create({
  baseURL,
  timeout: 100000,
  withCredentials: true,
  headers: {
    "Content-type": "application/json"
  }
});

export const tokenHandler = axiosToken(axios, {
  accessTokenKey: "access_token",
  refreshTokenKey: "refresh_token",
  accessTokenExpiresInKey: "expires_in",
  refreshTokenUrl: "/auth/refresh",
  onExpired() {
    tokenHandler.clearToken();
    // Logout from project or redirect to login page
  }
});

Step 2

In your signin or signup implementations pass sucessfull payload to setToken() method:

// for example
 const { mutate, isLoading } = useMutation(AuthApi.loginUser, {
    onSuccess: tokens => tokenHandler.setToken(tokens);
  });

Let's suppose your server returns following payload during user signin or signup:

{
    "token_type": "Bearer",
    "access_token": "yourJWTAccessToken",
    "refresh_token": "yourJWTRefreshToken",
    "expires_in": 86400
}

All token information are saved to sessionStorage by default.


Fields explanation:
|Field name | Default values | Description | |---- | ----- | ----- | | accessTokenKey | accessToken | this field per our response example should be changed to access_token | | refreshTokenKey | refreshToken | this field per our response example should be changed to refresh_token | | accessTokenExpiresInKey | accessTokenExpiresIn | this field per our response example should be changed to expires_in | | refreshTokenurl | an empty string | this field requires refresh token url path implemented in server. If server url provided as axios baseUrl it is enough to add relative path like /auth/refresh. |


Methods explanation: |Method name | Parameters | Description | |---- | ----- | ----- | | getToken() | access_token | refresh_token | or no argument | If no argument has been passed it returns object payload saved in Storage. Otherwise, it returns string of provided key value. | | setToken() | payload from server | On Signin or Signup just pass an object of token information. Example response has been provided above. | | refreshToken() | no arguments | In case where new tokens are required, just call this method and all tokens would be refreshed. | | clearToken() | no arguments | Clears token information from Storage. |


Callback methods explanation: |Method name | Return values | Description | |---- | ----- | ----- | | onExpired() | error | This is for controlling next step in case when refresh token expires. Usually in this case better to clear storage and forward to login/home pages. | | ontError() | error | If during handling process somthing goes wrong, then an error would be thrown for further catch at your side. |