1.0.4 • Published 5 years ago

token-management v1.0.4

Weekly downloads
42
License
MIT
Repository
github
Last release
5 years ago

INSTALL - npm: https://www.npmjs.com/package/token-management

npm i token-management

or

yarn add token-management

HOW IT WORK

import TokenManagement from './injectToken';

// test
let time = 1;

const tmn = new TokenManagement({
  isTokenValid(token) {
    if (time === 1) {
      ++time;
      return false;
    }

    return false;
  },
  getAccessToken() {
    return localStorage.getItem('accessToken');
  },
  onRefreshToken(done) {
    // call refresh token api

    console.log('----------------------------');
    console.log('refreshing');
    console.log('----------------------------');

    window.xz = () => done(12);
  },
});

export function injectToken(service) {
  return tmn.inject(service);
}


const testService = async token => {
  console.log('----------------------------');
  console.log('ok');
  console.log('----------------------------');

  return 'otken' + token;
};

const newInj = injectToken(testService);

(async () => {
  const [ val ] = await Promise.all([
    newInj(),
    newInj(),
    newInj(),
    newInj(),
    newInj(),
  ]);
  console.log('----------------------------');
  console.log(val, 'xyz');
  console.log('----------------------------');
})();