Giter Site home page Giter Site logo

mjsarfatti / axios-auth-refresh Goto Github PK

View Code? Open in Web Editor NEW

This project forked from flyrell/axios-auth-refresh

0.0 2.0 0.0 81 KB

Axios plugin which makes it very easy to automatically refresh the authorization tokens of your clients

JavaScript 100.00%

axios-auth-refresh's Introduction

axios-auth-refresh

Axios plugin that makes it easy to implement automatic refresh of authorization via axios' interceptors.

Size: 1.43 KiB minified, 1.06 KiB gzipped

Installation

Installing using npm or yarn

npm install axios-auth-refresh
# or
yarn add axios-auth-refresh

Syntax

createAuthRefreshInterceptor(axios, function refreshAuthLogic () {
    // Return a Promise
});

Parameters

  • axios - an instance of Axios used in project/call
  • refreshAuthLogic - a Function used for refreshing authentication (must return a promise)

Usage

In order to activate the interceptors, you need to import a function from axios-auth-refresh which is exported by default and call it with the axios instance you want the interceptors for, as well as the refresh authorization function where you need to write the logic for refreshing the authorization.

The interceptors will then be bind onto the axios instance and logic will be ran whenever 401 (Unauthorized) status is returned from a server. All new requests created while the refreshAuthLogic is processed will be bind onto the Promise returned from the refreshAuthLogic function.

import axios from 'axios';
import createAuthRefreshInterceptor from 'axios-auth-refresh';

// Function that will be called to refresh authorization
const refreshAuthLogic = () => axios.post('https://www.example.com/auth/token/refresh').then(res => {
    localStorage.setItem('token', res.data.token);
    return Promise.resolve();
});

// Instantiate the interceptor (you can chain it as it returns the axios instance)
createAuthRefreshInterceptor(axios, refreshAuthLogic);

// Make a call
axios.get('https://www.example.com/restricted/area')
    .then(/* ... */)
    .catch(/* ... */);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.