mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-03-07 00:09:13 +05:30
#246: remove outdated code from refreshTokenMiddleware
This commit is contained in:
parent
274c28a923
commit
87185b6e9b
@ -1,3 +1,4 @@
|
||||
import { getJwtPayload } from 'functions';
|
||||
import authentication from 'services/api/authentication';
|
||||
import logger from 'services/logger';
|
||||
import { updateToken, logoutAll } from 'components/accounts/actions';
|
||||
@ -14,7 +15,7 @@ import { updateToken, logoutAll } from 'components/accounts/actions';
|
||||
export default function refreshTokenMiddleware({dispatch, getState}) {
|
||||
return {
|
||||
before(req) {
|
||||
const {user, accounts} = getState();
|
||||
const {accounts} = getState();
|
||||
|
||||
let refreshToken;
|
||||
let token;
|
||||
@ -24,18 +25,15 @@ export default function refreshTokenMiddleware({dispatch, getState}) {
|
||||
if (accounts.active) {
|
||||
token = accounts.active.token;
|
||||
refreshToken = accounts.active.refreshToken;
|
||||
} else { // #legacy token
|
||||
token = user.token;
|
||||
refreshToken = user.refreshToken;
|
||||
}
|
||||
|
||||
if (!token || req.options.token || isRefreshTokenRequest) {
|
||||
return req;
|
||||
return Promise.resolve(req);
|
||||
}
|
||||
|
||||
try {
|
||||
const SAFETY_FACTOR = 300; // ask new token earlier to overcome time dissynchronization problem
|
||||
const jwt = getJWTPayload(token);
|
||||
const jwt = getJwtPayload(token);
|
||||
|
||||
if (jwt.exp - SAFETY_FACTOR < Date.now() / 1000) {
|
||||
return requestAccessToken(refreshToken, dispatch).then(() => req);
|
||||
@ -53,8 +51,8 @@ export default function refreshTokenMiddleware({dispatch, getState}) {
|
||||
|
||||
catch(resp, req, restart) {
|
||||
if (resp && resp.status === 401 && !req.options.token) {
|
||||
const {user, accounts} = getState();
|
||||
const {refreshToken} = accounts.active ? accounts.active : user;
|
||||
const {accounts} = getState();
|
||||
const {refreshToken} = accounts.active || {};
|
||||
|
||||
if (resp.message === 'Token expired' && refreshToken) {
|
||||
// request token and retry
|
||||
@ -83,16 +81,3 @@ function requestAccessToken(refreshToken, dispatch) {
|
||||
}
|
||||
|
||||
|
||||
function getJWTPayload(jwt) {
|
||||
const parts = (jwt || '').split('.');
|
||||
|
||||
if (parts.length !== 3) {
|
||||
throw new Error('Invalid jwt token');
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(atob(parts[1]));
|
||||
} catch (err) {
|
||||
throw new Error('Can not decode jwt token');
|
||||
}
|
||||
}
|
||||
|
@ -61,3 +61,24 @@ export const rAF = window.requestAnimationFrame
|
||||
* @param {bool} [immediate=false] - whether to execute at the beginning
|
||||
*/
|
||||
export debounce from 'debounce';
|
||||
|
||||
/**
|
||||
* @param {string} jwt
|
||||
*
|
||||
* @throws {Error} If can not decode token
|
||||
*
|
||||
* @return {object} - decoded jwt payload
|
||||
*/
|
||||
export function getJwtPayload(jwt) {
|
||||
const parts = (jwt || '').split('.');
|
||||
|
||||
if (parts.length !== 3) {
|
||||
throw new Error('Invalid jwt token');
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(atob(parts[1]));
|
||||
} catch (err) {
|
||||
throw new Error('Can not decode jwt token');
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import expect from 'unexpected';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import refreshTokenMiddleware from 'components/user/middlewares/refreshTokenMiddleware';
|
||||
|
||||
@ -75,9 +76,11 @@ describe('refreshTokenMiddleware', () => {
|
||||
const data = {url: '/refresh-token', options: {}};
|
||||
const resp = middleware.before(data);
|
||||
|
||||
expect(resp, 'to satisfy', data);
|
||||
|
||||
expect(authentication.requestToken, 'was not called');
|
||||
return expect(resp, 'to be fulfilled with', data)
|
||||
.then(() =>
|
||||
expect(authentication.requestToken, 'was not called')
|
||||
);
|
||||
});
|
||||
|
||||
it('should not auto refresh token if options.token specified', () => {
|
||||
@ -142,40 +145,6 @@ describe('refreshTokenMiddleware', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('when token expired legacy user', () => {
|
||||
beforeEach(() => {
|
||||
getState.returns({
|
||||
accounts: {
|
||||
active: null,
|
||||
available: []
|
||||
},
|
||||
user: {
|
||||
token: expiredToken,
|
||||
refreshToken
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should request new token', () => {
|
||||
const data = {
|
||||
url: 'foo',
|
||||
options: {
|
||||
headers: {}
|
||||
}
|
||||
};
|
||||
|
||||
authentication.requestToken.returns(Promise.resolve({token: validToken}));
|
||||
|
||||
return middleware.before(data).then((resp) => {
|
||||
expect(resp, 'to satisfy', data);
|
||||
|
||||
expect(authentication.requestToken, 'to have a call satisfying', [
|
||||
refreshToken
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should not be applied if no token', () => {
|
||||
getState.returns({
|
||||
accounts: {
|
||||
@ -187,9 +156,10 @@ describe('refreshTokenMiddleware', () => {
|
||||
const data = {url: 'foo'};
|
||||
const resp = middleware.before(data);
|
||||
|
||||
expect(resp, 'to satisfy', data);
|
||||
|
||||
expect(authentication.requestToken, 'was not called');
|
||||
return expect(resp, 'to be fulfilled with', data)
|
||||
.then(() =>
|
||||
expect(authentication.requestToken, 'was not called')
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -290,25 +260,5 @@ describe('refreshTokenMiddleware', () => {
|
||||
expect(authentication.requestToken, 'was not called');
|
||||
});
|
||||
});
|
||||
|
||||
describe('legacy user.refreshToken', () => {
|
||||
beforeEach(() => {
|
||||
getState.returns({
|
||||
accounts: {
|
||||
active: null
|
||||
},
|
||||
user: {refreshToken}
|
||||
});
|
||||
});
|
||||
|
||||
it('should request new token if expired', () =>
|
||||
middleware.catch(expiredResponse, {options: {}}, restart).then(() => {
|
||||
expect(authentication.requestToken, 'to have a call satisfying', [
|
||||
refreshToken
|
||||
]);
|
||||
expect(restart, 'was called');
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user