Code Monkey home page Code Monkey logo

reactjs-handle-refresh-token's Introduction

Handle refresh token with axios, umi-request using interceptors, apollo-client, token-management, brainless-token-manager

1. Axios interceptors, Apollo-client

  • After all requests failed, we will call a request to take a new access token after that retry all requests which failed

alt text

2. brainless-token-manager

  • Check access token expire if token expire will call a request to take a new access token after that call requests

brainless-token-manager

alt text

reactjs-handle-refresh-token's People

Contributors

hunghg255 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

reactjs-handle-refresh-token's Issues

Trường hợp refresh token ko thành công (api trả về mã 400) không trigger onInvalidRefreshToken()

token manager :

  getAccessToken: async () => {
    const token = getCookie(APP_SAVE_KEY.TOKEN_KEY);
    return `${token}`;
  },
  getRefreshToken: async () => {
    const refreshToken = getCookie(APP_SAVE_KEY.REFRESH_TOKEN_KEY);
    return `${refreshToken}`;
  },
  onInvalidRefreshToken: () => {
    notification.error({ message: 'Your session expired!', placement: 'top' });
    window.location.href = '/login';
    deleteCookie(APP_SAVE_KEY.TOKEN_KEY);
    deleteCookie(APP_SAVE_KEY.REFRESH_TOKEN_KEY);
  },
  executeRefreshToken: async () => {
    const refreshToken = getCookie(APP_SAVE_KEY.REFRESH_TOKEN_KEY);
    if (!refreshToken) {
      return {
        token: '',
        refresh_token: '',
      };
    }
    const r = await axios.post('oauth2/token', `grant_type=refresh_token&refresh_token=${refreshToken}`, {
      baseURL : process.env.NEXT_PUBLIC_DEV_API_URL,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Authorization: 'Basic d2ViQXBwQ2xpZW50OnZuYS1jbGllbnQ',
      },
    });

    return {
      token: r.data?.access_token,
      refresh_token: r.data?.refresh_token,
    };
  },
  onRefreshTokenSuccess: ({ token, refresh_token }) => {
    if (token && refresh_token) {
      setCookie(APP_SAVE_KEY.TOKEN_KEY, token);
      setCookie(APP_SAVE_KEY.REFRESH_TOKEN_KEY, refresh_token);
    }
  },
});

mong tác giả support

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.